Generate JSON schema from Java class

前端 未结 3 633
孤城傲影
孤城傲影 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条回答
  •  梦毁少年i
    2021-02-03 18:47

    public static String getJsonSchema(Class clazz) throws IOException {
             Field[] fields = clazz.getDeclaredFields();
             List> map=new ArrayList>();
             for (Field field : fields) {
                 HashMap objMap=new  HashMap();
                 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;
        }
    

提交回复
热议问题