首页 > 解决方案 > 从 Mongo 集合创建 Java POJO

问题描述

我有一个现有的 MongoDB,我需要为所有集合编写 Java POJO。

是否有任何工具可以从 mongo 集合中自动生成 POJO?

我能够找到将 Mongo 集合转换为 JSON 的工具,但找不到将集合转换为 Java POJO 的合适方法。

标签: javamongodbpojogenerateautocreate

解决方案


使用http://www.jsonschema2pojo.org/

像这样的json:

{
  "type":"object",
  "working":true,
  "id":1
}

会产生

-----------------------------------com.example.Example.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Example {

@SerializedName("type")
@Expose
private String type;
@SerializedName("working")
@Expose
private Boolean working;
@SerializedName("id")
@Expose
private Long id;

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public Boolean getWorking() {
return working;
}

public void setWorking(Boolean working) {
this.working = working;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

}

推荐阅读