首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >从Java类生成JSON模式

从Java类生成JSON模式
EN

Stack Overflow用户
提问于 2014-10-05 12:50:20
回答 4查看 92.9K关注 0票数 47

我有一个POJO类:

代码语言:javascript
复制
public class Stock {
 int id;
 String name;
 Date date;
}

是否有任何注释或开发框架/API可以将POJO转换为JSON模式,如下所示:

代码语言:javascript
复制
{"id":
      {             
        "type" : "int"
      },
"name":{   
        "type" : "string"
       }
"date":{
        "type" : "Date"
      }
}

我还可以通过在POJO上指定一些注释或配置来扩展模式,以添加"Required" : "Yes"、每个字段的描述等信息,并可以生成如下所示的JSON schema:

代码语言:javascript
复制
{"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"
      }
}
EN

回答 4

Stack Overflow用户

发布于 2014-10-24 05:00:44

Jackson JSON Schema模块就是这样一个工具:

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

它使用Jackson数据库的POJO内省来遍历POJO属性,并考虑到Jackson注释,并生成一个JSON Schema对象,然后可以将该对象序列化为JSON或用于其他目的。

票数 12
EN

Stack Overflow用户

发布于 2021-03-16 06:24:55

Java JSON Schema Generator:https://github.com/victools/jsonschema-generator

使用Jackson从Java类创建JSON Schema (草案6、草案7或草案2019-09)。

票数 6
EN

Stack Overflow用户

发布于 2015-11-06 02:55:13

使用JJschema。它可以生成符合草案4的JSON模式。有关详细信息,请参阅此帖子http://wilddiary.com/generate-json-schema-from-java-class/

虽然Jackson Json Schema module也可以生成模式,但到目前为止,它只能生成符合草案3的模式。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26199716

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档