我想在我的项目中从pojos自动生成JsonSchema:代码如下所示:
ObjectMapper mapper = new ObjectMapper();
SchemaFactoryWrapper visitor = new SchemaFactoryWrapper();
mapper.acceptJsonFormatVisitor(clazz, visitor);
JsonSchema jsonSchema = visitor.finalSchema();
String schemaString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonSchema);
当clazz定义如下:
public class ZKBean
{
public String anExample;
public int anInt;
}
I end up with this:
{
"type" : "object",
"id" : "urn:jsonschema:com:emc:dpad:util:ZKBean",
"properties" : {
"anInt" : {
"type" : "integer"
},
"anExample" : {
"type" : "string"
}
}
}
一切都很棒.我想要做的是在模式中添加“description”键,以便我有一些看起来像:
{
"type" : "object",
"id" : "urn:jsonschema:com:emc:dpad:util:ZKBean",
"properties" : {
"anInt" : {
"type" : "integer",
"description" : "Represents the number of foos in the system"
},
"anExample" : {
"type" : "string",
"description" : "Some descriptive description goes here"
}
}
}
我假设有一些注释我可以放在我的ZKBean类中的字段上,但经过半天的未来之后,我还没找到一个.这是要走的路吗?或者我需要与访客做些什么?
谢谢,
杰西
解决方法:
您可以使用@JsonPropertyDescription批注生成自Jackson 2.4.1以来可用的json模式.这是一个例子:
public class JacksonSchema {
public static class ZKBean {
@JsonPropertyDescription("This is a property description")
public String anExample;
public int anInt;
}
public static void main(String[] args) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
SchemaFactoryWrapper visitor = new SchemaFactoryWrapper();
mapper.acceptJsonFormatVisitor(ZKBean.class, visitor);
JsonSchema jsonSchema = visitor.finalSchema();
System.out.println(mapper
.writerWithDefaultPrettyPrinter().writeValueAsString(jsonSchema));
}
}
输出:
{
"type" : "object",
"id" : "urn:jsonschema:*:JacksonSchema:ZKBean",
"properties" : {
"anExample" : {
"type" : "string",
"description" : "This is a property description"
},
"anInt" : {
"type" : "integer"
}
}
}