I am trying to automatically generate JsonSchema from pojos in my project: The code looks like this:
我试图在我的项目中从pojos自动生成JsonSchema:代码如下所示:
ObjectMapper mapper = new ObjectMapper();
SchemaFactoryWrapper visitor = new SchemaFactoryWrapper();
mapper.acceptJsonFormatVisitor(clazz, visitor);
JsonSchema jsonSchema = visitor.finalSchema();
String schemaString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonSchema);
When clazz is defined like this:
当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"
}
}
}
All that is great. What I want to do is add the "description" key to the schema, so that I instead have something that looks like:
一切都很棒。我想要做的是将“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"
}
}
}
I assumed there was some annotation I could just put on the fields in my ZKBean class, but after half a day of futzing I have not found one. Is this the way to go? Or do I need to do something with my Visitor?
我假设有一些注释我可以放在我的ZKBean类中的字段上,但是经过半天的充实,我还没找到一个。这是要走的路吗?或者我需要与访客做些什么?
Thanks, Jesse
3 个解决方案
#1
4
You can use the @JsonPropertyDescription
annotation for generating json schema which works since Jackson 2.4.1. Here is an example:
您可以使用@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));
}
}
Output:
{
"type" : "object",
"id" : "urn:jsonschema:*:JacksonSchema:ZKBean",
"properties" : {
"anExample" : {
"type" : "string",
"description" : "This is a property description"
},
"anInt" : {
"type" : "integer"
}
}
}
#2
0
It appears that the problem lies not in the jackson libraries, but in the package I am using to generate the class objects. My code is pasted below; I suspect the Reflections object is dropping the annotation information (when I manually specify ZKBean.class the description appears). Thanks for the help!
似乎问题不在于jackson库,而是在我用来生成类对象的包中。我的代码粘贴在下面;我怀疑Reflections对象正在删除注释信息(当我手动指定ZKBean.class时,会出现描述)。谢谢您的帮助!
Reflections reflections = new Reflections(
new ConfigurationBuilder().setUrls(
ClasspathHelper.forClassLoader(urlcl)).
addClassLoader(urlcl));
Set<Class<?>> annotated = reflections.getTypesAnnotatedWith(JsonBean.class);
ObjectMapper mapper = new ObjectMapper();
SchemaFactoryWrapper visitor = new SchemaFactoryWrapper();
for (Class<?> clazz : annotated)
{
try
{
mapper.acceptJsonFormatVisitor(mapper.constructType(SampleBean.class), visitor);
JsonSchema jsonSchema = visitor.finalSchema();
String schemaString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonSchema);
System.out.println(schemaString);
#3
0
I had the same problem and identified another possible cause (just adding in hope this might help someone).
我有同样的问题,并确定了另一个可能的原因(只是希望这可能有助于某人)。
I am also loading Class objects (of DTO POJOs with Jackson annotations) using a custom ClassLoader and have them processed by ObjectMapper and SchemaFactoryWrapper to generate corresponding JSON schemas. In my case I forgot to add the jackson-annotations jar to the ClassLoader which leads to the described problem.
我还使用自定义ClassLoader加载Class对象(带有Jackson注释的DTO POJO),并由ObjectMapper和SchemaFactoryWrapper处理它们以生成相应的JSON模式。在我的情况下,我忘了将Jackson-annotations jar添加到ClassLoader,这导致了所描述的问题。
#1
4
You can use the @JsonPropertyDescription
annotation for generating json schema which works since Jackson 2.4.1. Here is an example:
您可以使用@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));
}
}
Output:
{
"type" : "object",
"id" : "urn:jsonschema:*:JacksonSchema:ZKBean",
"properties" : {
"anExample" : {
"type" : "string",
"description" : "This is a property description"
},
"anInt" : {
"type" : "integer"
}
}
}
#2
0
It appears that the problem lies not in the jackson libraries, but in the package I am using to generate the class objects. My code is pasted below; I suspect the Reflections object is dropping the annotation information (when I manually specify ZKBean.class the description appears). Thanks for the help!
似乎问题不在于jackson库,而是在我用来生成类对象的包中。我的代码粘贴在下面;我怀疑Reflections对象正在删除注释信息(当我手动指定ZKBean.class时,会出现描述)。谢谢您的帮助!
Reflections reflections = new Reflections(
new ConfigurationBuilder().setUrls(
ClasspathHelper.forClassLoader(urlcl)).
addClassLoader(urlcl));
Set<Class<?>> annotated = reflections.getTypesAnnotatedWith(JsonBean.class);
ObjectMapper mapper = new ObjectMapper();
SchemaFactoryWrapper visitor = new SchemaFactoryWrapper();
for (Class<?> clazz : annotated)
{
try
{
mapper.acceptJsonFormatVisitor(mapper.constructType(SampleBean.class), visitor);
JsonSchema jsonSchema = visitor.finalSchema();
String schemaString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonSchema);
System.out.println(schemaString);
#3
0
I had the same problem and identified another possible cause (just adding in hope this might help someone).
我有同样的问题,并确定了另一个可能的原因(只是希望这可能有助于某人)。
I am also loading Class objects (of DTO POJOs with Jackson annotations) using a custom ClassLoader and have them processed by ObjectMapper and SchemaFactoryWrapper to generate corresponding JSON schemas. In my case I forgot to add the jackson-annotations jar to the ClassLoader which leads to the described problem.
我还使用自定义ClassLoader加载Class对象(带有Jackson注释的DTO POJO),并由ObjectMapper和SchemaFactoryWrapper处理它们以生成相应的JSON模式。在我的情况下,我忘了将Jackson-annotations jar添加到ClassLoader,这导致了所描述的问题。