使用Jackson从POJO数组生成JSON模式

yar*_*fed 1 java arrays json jsonschema jackson

我想使用Jackson 2.7.0从POJO数组生成JSON模式:

JsonSchema schema = mapper.generateJsonSchema(MyClass[].class);
Run Code Online (Sandbox Code Playgroud)

所以我希望得到JSON模式:

{
  "type": "array",
  "items": {
    "type": "object",
    "properties": {
      "id": {
        "type": "string"
      },
      "text": {
        "type": "string"
      }
  ...
Run Code Online (Sandbox Code Playgroud)

但是得到:

{
      "type": "array"
}
Run Code Online (Sandbox Code Playgroud)

如何获得预期的架构?
是否存在任何更好的库可以从POJO生成模式?
在此先感谢您的帮助!

Dan*_*aub 6

JsonSchema自version以来不推荐使用2.2
如果您使用gradle或maven则将其jackson-module-jsonSchema用作依赖项

compile "com.fasterxml.jackson.module:jackson-module-jsonSchema:2.9.0
Run Code Online (Sandbox Code Playgroud)

然后,您可以按以下方式创建模式:

ObjectMapper mapper = new ObjectMapper();

JsonSchemaGenerator schemaGen = new JsonSchemaGenerator(mapper);
JsonSchema schema = schemaGen.generateSchema(MyClass[].class);

String schemaText = mapper.writeValueAsString(schema);
Run Code Online (Sandbox Code Playgroud)

这对你有用