当 POJO/Bean 在其中嵌套参数时,我对此并不陌生,并且正在为用例而苦苦挣扎。
要求-
使用 Jackson(最新版本可以)为 JAVA Bean/POJO 类生成 JSON 模式,以便它正确包含嵌套对象的结构,并且还希望向嵌套的 pojos 添加自定义属性(在我的情况下,想要添加一个每个嵌套 POJO 参数的完全分类的类名属性)。
用例-
说,我有一个 Person 类,如下所示。我正在使用这个人作为我的一些操作的参数。-
public class Person {
private String name;
private String id;
private int i;
private Person2 p;
private List<String> strList;
private HashMap<String, String> strMap;
private Person3[] p3;
public void setName(String name){
this.name = name;
}
public void setId(String id){
this.id = id;
}
public void setI(int i){
this.i = i;
}
public void setP(Person2 p){
this.p = p;
}
public String getName(){
return this.name;
}
public String getId(){
return this.id;
}
public int getI(){
return this.i;
}
public Person2 getP(){
return this.p;
}
public void setStrList(List<String> strList){
this.strList = strList;
}
public List<String> getStrList(){
return this.strList;
}
public void setStrMap(HashMap<String, String> strMap){
this.strMap = strMap;
}
public HashMap<String, String> getStrMap(){
return this.strMap;
}
public void setP3(Person3[] p3){
this.p3 = p3;
}
public Person3[] getP3(){
return this.p3;
}
}
例如,当上面的 Person 类用作参数时,当前会生成以下 JSON Schema -
{
"type": "object",
"properties": {
"name": {
"type": "string"
},
"id": {
"type": "string"
},
"i": {
"type": "integer"
},
"p": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"id": {
"type": "string"
},
"i": {
"type": "integer"
},
"p1": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"id": {
"type": "string"
},
"i": {
"type": "integer"
}
}
}
}
}
},
"strList": {
"type": "array",
"items": {
"type": "string"
}
},
"strMap": {
"type": "object"
},
"p3": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"id": {
"type": "string"
},
"i": {
"type": "integer"
}
}
}
}
},
"classname": "com.agent.Person"
}
Person 类有一些多值数据结构,如MAP、ARRAY,也可以有嵌套的POJO。所以我不想为这些类型的BEAN/POJO类生成 JSON 模式,并且还想为每个嵌套的POJO/BEAN放置一个“类名”节点,具有完全分类的类名。
我为此经历了很多事情,但我无法为使用杰克逊的这种情况找出一个捷径。
这里要注意的要求是将“classname”属性放在嵌套的 POJO 属性模式中。
这个问题肯定与此有关 - How to traverse generated json schema using jackson and put custom attribute in json schema