Generate JSON schema from Java class

前端 未结 3 629
孤城傲影
孤城傲影 2021-02-03 18:18

I have a POJO class:

public class Stock {
 int id;
 String name;
 Date date;
}

Are there any annotations or development framework/API that can co

相关标签:
3条回答
  • 2021-02-03 18:39

    One such tool is Jackson JSON Schema module:

    https://github.com/FasterXML/jackson-module-jsonSchema

    which uses Jackson databind's POJO introspection to traverse POJO properties, taking into account Jackson annotations, and produces a JSON Schema object, which may then be serialized as JSON or used for other purposes.

    0 讨论(0)
  • 2021-02-03 18:47
    public static String getJsonSchema(Class clazz) throws IOException {
             Field[] fields = clazz.getDeclaredFields();
             List<Map<String,String>> map=new ArrayList<Map<String,String>>();
             for (Field field : fields) {
                 HashMap<String, String> objMap=new  HashMap<String, String>();
                 objMap.put("name", field.getName());
                 objMap.put("type", field.getType().getSimpleName());
                 objMap.put("format", "");
                 map.add(objMap);
             }
             ObjectMapper mapper = new ObjectMapper();
             String json = mapper.writeValueAsString(map);
    
           return json;
        }
    
    0 讨论(0)
  • 2021-02-03 19:02

    Use JJschema. It can generate draft 4 compliant JSON schemas. Refer this post http://wilddiary.com/generate-json-schema-from-java-class/ for details.

    Though Jackson Json Schema module can too generate schema but it can, as of today, only generate draft 3 compliant schemas only.

    0 讨论(0)
提交回复
热议问题