私はこれが初めてで、POJO/Bean にパラメーターがネストされている場合のユースケースに苦労しています。
要件-
ネストされたオブジェクトの構造を適切に含み、ネストされた pojo にカスタム属性を追加したい (私の場合は、ネストされた POJO パラメータごとに完全に分類された classname 属性)。
ユースケース-
たとえば、次の 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 スキーマを生成します -
{
"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 スキーマを生成したくありません。また、完全に分類された classname を持つ、ネストされたPOJO/BEANごとに「classname」ノードを配置したいと考えています。
私はこれについて多くのことを経験していますが、Jackson を使用してこの種の状況の省略形を理解することができません。
ここで注意すべき要件は、ネストされた POJO 属性スキーマに「classname」属性を配置することです。
この質問は間違いなくこれに関連しています - How to traverse generated json schema using jackson and put custom attribute in json schema