如何使用所需的提交或默认值生成JSON模式?
我正在使用Jackson处理JSON对象,我想要一种从可以在对象上执行的类中生成架构的方法。 我发现我可以使用以下方法:
JsonSchemaGenerator generator = new JsonSchemaGenerator(mapper);
JsonSchema jsonSchema = generator.generateSchema(DBUser.class);
mapper.writeValueAsString(jsonSchema);
使用此com.fasterxml.jackson.module.jsonschema.jsonschemagenerator。
从这个类生成:
public class DBUser {
private String database="stu";
private int role;
private String username;
private String password;
//setters and getters defined
}
给我这一点:
{
"type" : "object",
"id" : "urn:jsonschema:DBUser",
"properties" : {
"database" : {
"type" : "string"
},
"role" : {
"type" : "integer"
},
"username" : {
"type" : "string"
},
"password" : {
"type" : "string"
}
}
}
我需要的是这样的必需字段:
"required": ["database","role","username"]
但是它没有必要的字段或初始值,我需要。 那我怎么能得到这个呢?
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.
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.
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以用
jsonproperty#oilder
设置为true
以生成带有必需字段的JSonschema:请注意,请注意,请注意文档 JSonschema模块支持创建JSON Schema(v3),而不是上层JSON(v3)模式版本,因此不支持以后版本中引入的关键字。
You can annotate your pojo
DBUser
class fields with theJsonProperty#required
set totrue
to generate a jsonschema with required fields: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.