Generate JSON schema from Java class

前端 未结 3 1756
栀梦
栀梦 2021-02-03 18:10

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:57

    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;
        }
    

提交回复
热议问题