简体   繁体   English

如何从 Java Class 生成 JsonSchema 作为数组

[英]How to generate JsonSchema As Array from Java Class

I'm generating JsonSchema from Java class using fasterxml.jackson.我使用fastxml.jackson从Java class生成JsonSchema。 The generated Jsonschema will be as below生成的 Jsonschema 如下

    {
    "type": "object",
    "properties": {
      "id": {
        "type": "string"
      },
      "name": {
        "type": "string"
      }
     }
    }

My code to generate JsonSchema我生成 JsonSchema 的代码

public static String getJsonSchema(Class definitionClass) throws JsonProcessingException {
        ObjectMapper mapper =  new ObjectMapper().disable(
                MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS);
        JavaType javaType = mapper.getTypeFactory().constructType(definitionClass);
        JsonSchemaGenerator schemaGen = new JsonSchemaGenerator(mapper);
        JsonSchema schema = schemaGen.generateSchema(javaType);
        return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(schema);
    }

I tried to use ArraySchema arraySchema = schema.asArraySchema();我尝试使用ArraySchema arraySchema = schema.asArraySchema(); but it generates invalid schema.但它会生成无效的架构。 My expected JsonSchema should be like below我预期的 JsonSchema 应该如下所示

  {
  "type": "array",
  "items": {
    "type": "object",
    "properties": {
      "id": {
        "type": "string"
      },
      "name": {
        "type": "string"
      }
     }
    }
   }

TL;DR: Call getJsonSchema(MyClass[].class) TL;DR:调用getJsonSchema(MyClass[].class)


Works fine if we simply tell it to generate the schema of a Java array.如果我们简单地告诉它生成 Java 数组的模式,则工作正常。

To demo, I created a MyClass class fitting the shown schema.为了演示,我创建了一个符合所示架构的MyClass class。 I used public fields for simplicity of the demo, but we'd use private fields and public getter/setter methods in real life.为了简化演示,我使用了public字段,但在现实生活中我们会使用private字段和public getter/setter 方法。

class MyClass {
    public String id;
    public String name;
}

Now, to show that we get the same result as in the question, we call it with MyClass.class :现在,为了表明我们得到与问题相同的结果,我们使用MyClass.class调用它:

System.out.println(getJsonSchema(MyClass.class));

Output Output

{
  "type" : "object",
  "id" : "urn:jsonschema:MyClass",
  "properties" : {
    "id" : {
      "type" : "string"
    },
    "name" : {
      "type" : "string"
    }
  }
}

Now, we want the schema to be an array of those, so we will instead call it using MyClass[].class .现在,我们希望架构是一个数组,所以我们将改为使用MyClass[].class来调用它。

System.out.println(getJsonSchema(MyClass[].class));

Output Output

{
  "type" : "array",
  "items" : {
    "type" : "object",
    "id" : "urn:jsonschema:MyClass",
    "properties" : {
      "id" : {
        "type" : "string"
      },
      "name" : {
        "type" : "string"
      }
    }
  }
}

The above was tested using jackson-module-jsonSchema-2.10.3.jar .以上是使用jackson-module-jsonSchema-2.10.3.jar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM