我有一个 POJO 类:
public class Stock {
int id;
String name;
Date date;
}
是否有任何注释或开发框架/API 可以将 POJO 转换为 JSON 模式,如下所示:
{"id":
{
"type" : "int"
},
"name":{
"type" : "string"
}
"date":{
"type" : "Date"
}
}
我还可以扩展模式以添加信息,如“必需”:"is"
,每个字段的描述等,通过在POJO上指定一些注释或配置,并且可以生成JSON模式如下:
{"id":
{
"type" : "int",
"Required" : "Yes",
"format" : "id must not be greater than 99999",
"description" : "id of the stock"
},
"name":{
"type" : "string",
"Required" : "Yes",
"format" : "name must not be empty and must be 15-30 characters length",
"description" : "name of the stock"
}
"date":{
"type" : "Date",
"Required" : "Yes",
"format" : "must be in EST format",
"description" : "filing date of the stock"
}
}
最佳答案
Jackson JSON Schema 模块就是一个这样的工具:
https://github.com/FasterXML/jackson-module-jsonSchema
它使用 Jackson 数据绑定(bind)的 POJO 自省(introspection)来遍历 POJO 属性,同时考虑到 Jackson 注释,并生成 JSON Schema 对象,然后可以将其序列化为 JSON 或用于其他目的。
关于json - 从 Java 类生成 JSON 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26199716/