2

当 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 类有一些多值数据结构,如MAPARRAY,也可以有嵌套的POJO所以我不想为这些类型的BEAN/POJO类生成 JSON 模式,并且还想为每个嵌套的POJO/BEAN放置一个“类名”节点,具有完全分类的类名

我为此经历了很多事情,但我无法为使用杰克逊的这种情况找出一个捷径。

这里要注意的要求是将“classname”属性放在嵌套的 POJO 属性模式中。

这个问题肯定与此有关 - How to traverse generated json schema using jackson and put custom attribute in json schema

4

1 回答 1

0

这可能是上述问题的方法之一,可能如下 -

方法一

这是我将类名放入生成的模式的代码。此代码处理提供数组或非数组参数的情况。即 Person.class 和 Person[].class 可以成功处理。此代码无法处理杰克逊上仍然存在的自我参考问题 - https://github.com/FasterXML/jackson-databind/issues/339

下面的代码可以实例化如下 -

public static void main(String[] args) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        Class<?> cls = Person[].class;
        if(cls.isArray()){
            cls = cls.getComponentType();
        }
        String s = "{\"rootNode\":{\"classname\":\"" + cls.getName() + "\"},"
                + getAttributeClassnames(cls) + "}";
        s = s.replace("\",}", "\"}").replace("},}", "}}");
        System.out.println(s);
        s = mapper.generateJsonSchema(cls).getSchemaNode().put("type", "array")
                .put("classnames", s).toString();
        s = s.replace("\\", "").replace("\"{", "{").replace("}\"", "}");        
        System.out.println(s);
    }

static String getAttributeClassnames(Class<?> cls) {
    String s = "";      
        Field[] field = cls.getDeclaredFields();
        int i = 0;
        while (i < field.length) {              
            if (!(field[i].getType() == Boolean.class)
                    && !(field[i].getType() == Integer.class)
                    && !(field[i].getType() == Character.class)
                    && !(field[i].getType() == Byte.class)
                    && !(field[i].getType() == Short.class)
                    && !(field[i].getType() == Long.class)
                    && !(field[i].getType() == Float.class)
                    && !(field[i].getType() == Double.class)
                    && !(field[i].getType().isPrimitive())
                    && !(field[i].getType() == String.class)
                    && !(Collection.class.isAssignableFrom(field[i]
                            .getType()))
                    && !(Map.class.isAssignableFrom(field[i].getType()))
                    && !(Arrays.class.isAssignableFrom(field[i].getType()))) {
                if(field[i].getType() == cls){
                    if (i == field.length - 1) {
                        Class<?> name = null;
                        if(field[i].getType().isArray()){
                            name = field[i].getType().getComponentType();
                        }else{
                            name = field[i].getType();
                        }
                        s = s + "\"" + field[i].getName() + "\""
                                + ":{\"classname\":\""
                                + name.getName() + "\","
                                +"}";
                    } else {
                        Class<?> name = null;                       
                        if(field[i].getType().isArray()){
                            name = field[i].getType().getComponentType();
                        }else{
                            name = field[i].getType();
                        }
                        s = s + "\"" + field[i].getName() + "\""
                                + ":{\"classname\":\""
                                + name.getName() + "\","
                                + "}" + ",";
                    }
                
                }else{
                    if (i == field.length - 1) {
                        Class<?> name = null;
                        if(field[i].getType().isArray()){
                            name = field[i].getType().getComponentType();
                        }else{
                            name = field[i].getType();
                        }
                        s = s + "\"" + field[i].getName() + "\""
                                + ":{\"classname\":\""
                                + name.getName() + "\","
                                + getAttributeClassnames(name)
                                + "}";
                    } else {
                        Class<?> name = null;                       
                        if(field[i].getType().isArray()){
                            name = field[i].getType().getComponentType();
                        }else{
                            name = field[i].getType();
                        }
                        s = s + "\"" + field[i].getName() + "\""
                                + ":{\"classname\":\""
                                + name.getName() + "\","
                                + getAttributeClassnames(name)
                                + "}" + ",";
                    }
                }
            }
            i++;
        }
    return s;
}

方法二

其他方法可能是维护杰克逊生成的模式的类名图。

例如

"classnames":{
          "<attribute_name>":{
                                "classname":"<classname>"
                             }
}
于 2014-09-24T15:35:58.520 回答