use*_*174 37 json jsonschema jackson fasterxml jackson-modules
我有一个POJO课程
public class Stock{
int id;
String name;
Date date;
}
Run Code Online (Sandbox Code Playgroud)
是否有任何注释或开发框架/ api可以将POJO转换为JSON模式,如下所示
{"id":
{
"type" : "int"
},
"name":{
"type" : "string"
}
"date":{
"type" : "Date"
}
}
Run Code Online (Sandbox Code Playgroud)
并且我可以通过在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"
}
}
Run Code Online (Sandbox Code Playgroud)
Sto*_*wke 26
我自己需要这样做,但需要获得最新的架构规范(截至本文的v4).我的解决方案是以下链接的第一个答案: 从POJO生成Json Schema
使用org.codehaus.jackson.map包中的对象而不是com.fasterxml.jackson.databind包.如果你下面就说明这个页面,那么你就错了.只需使用jackson-mapper模块即可.
以下是未来googlers的代码:
private static String getJsonSchema(Class clazz) throws IOException {
org.codehaus.jackson.map.ObjectMapper mapper = new ObjectMapper();
//There are other configuration options you can set. This is the one I needed.
mapper.configure(SerializationConfig.Feature.WRITE_ENUMS_USING_TO_STRING, true);
JsonSchema schema = mapper.generateJsonSchema(clazz);
return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(schema);
}
Run Code Online (Sandbox Code Playgroud)
Edg*_*ues 14
Java JSON 模式生成器:https://github.com/victools/jsonschema-generator
使用 Jackson 从 Java 类创建 JSON 架构(Draft 6、Draft 7 或 Draft 2019-09)。
Sta*_*Man 12
其中一个工具是Jackson JSON Schema模块:
https://github.com/FasterXML/jackson-module-jsonSchema
它使用Jackson数据绑定的POJO内省来遍历POJO属性,同时考虑Jackson注释,并生成一个JSON Schema对象,然后可以将其序列化为JSON或用于其他目的.
| 归档时间: |
|
| 查看次数: |
63723 次 |
| 最近记录: |