简体   繁体   English

如何生成具有所需字段或默认值的 JSON 模式?

[英]How can i generate JSON schema with a required filed or default values?

im using jackson to handle JSON objects, and i want a way to generate a schema from a class that i can enforce on the objects.我使用杰克逊来处理 JSON 对象,我想要一种方法来从我可以对对象强制执行的类生成模式。 i found out that i can use this :我发现我可以使用这个:

JsonSchemaGenerator generator = new JsonSchemaGenerator(mapper);
JsonSchema jsonSchema = generator.generateSchema(DBUser.class);
mapper.writeValueAsString(jsonSchema);

using this com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator.使用这个 com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator。
generated from this class:从这个类生成:

public class DBUser {

    private String database="stu";
    private int role;
    private String username;
    private String password;
    //setters and getters defined
    }

which gives me this :这给了我这个:

{
  "type" : "object",
  "id" : "urn:jsonschema:DBUser",
  "properties" : {
    "database" : {
      "type" : "string"
    },
    "role" : {
      "type" : "integer"
    },
    "username" : {
      "type" : "string"
    },
    "password" : {
      "type" : "string"
    }
  }
}

what i need is a required field like this :我需要的是这样的必填字段:

"required": ["database","role","username"]

but it has no required field, or initial values, which i need.但它没有我需要的必填字段或初始值。 so how can i get that ?那我怎么能得到呢?

You can annotate your pojo DBUser class fields with the JsonProperty#required set to true to generate a jsonschema with required fields:您可以使用JsonProperty#required设置为true来注释您的 pojo DBUser类字段,以生成带有必填字段的 jsonschema:

public class DBUser {

    @JsonProperty(required = true)
    private String database = "stu";
    @JsonProperty(required = true)
    private int role;
    @JsonProperty(required = true)
    private String username;
    private String password;
}

//generate the new jsonschema with the required fields
JsonSchemaGenerator generator = new JsonSchemaGenerator(mapper);
JsonSchema jsonSchema = generator.generateSchema(DBUser.class);
System.out.println(mapper.writerWithDefaultPrettyPrinter()
                         .writeValueAsString(jsonSchema));


//json generated
{
  "type" : "object",
  "id" : "urn:jsonschema:DBUser",
  "properties" : {
    "database" : {
      "type" : "string",
      "required" : true
    },
    "role" : {
      "type" : "integer",
      "required" : true
    },
    "username" : {
      "type" : "string",
      "required" : true
    },
    "password" : {
      "type" : "string"
    }
  }
}

Note that according to the documentation the jsonschema module supports the creation of a JSON Schema (v3) and not upper JSON Schema versions, so keywords introduced in later versions are not supported.请注意,根据文档,jsonschema 模块支持创建 JSON Schema (v3) 而不是更高的 JSON Schema 版本,因此不支持更高版本中引入的关键字。

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

相关问题 如何将 JSON 模式中的默认值添加到 Java 中的 JSON 文档 - How to add default values from JSON schema to JSON document in Java Spring Data Rest:如何使json架构属性成为必需? - Spring Data Rest: How can I make a json schema property required? 当我只得到一个字符串时,如何从带有泛型的Java类生成JSON模式? - How can I generate a JSON Schema from a Java class with generics when I am only given a string? Jackson:如何生成拒绝所有其他内容的 json 模式 - Jackson: How can I generate json schema which rejects all additional content 我如何从mongodb中生成json模式在Java中的结果? - How i generate json schema from mongodb result in java? 无法从POJO生成Json模式 - Can't Generate Json schema from POJO 如何在将 JSON 中的 LocalDateTime 解析为具有相同字段的对象后比较 JSON 中的 LocalDateTime 并在比较中忽略毫秒? - How can I compare LocalDateTime in JSON after parsing it out of an Object with the same filed in the object and ignore millisecond in the comparing? 如何告诉jaxb / Maven生成多个模式包? - How can I tell jaxb / Maven to generate multiple schema packages? 如何使用 json 模式生成内部 Java 类? - How to generate inner java classes with json schema? 如何在JAVA中以所需格式生成json字符串? - How to generate json string in required format in JAVA?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM