Generating JsonSchema from pojo: how do I add “des

2020-07-11 10:58发布

问题:

I am trying to automatically generate JsonSchema from pojos in my project: The code looks like this:

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:

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:

{
  "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?

Thanks, Jesse

回答1:

You can use the @JsonPropertyDescription annotation for generating json schema which works since Jackson 2.4.1. Here is an example:

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:stackoverflow:JacksonSchema:ZKBean",
  "properties" : {
    "anExample" : {
      "type" : "string",
      "description" : "This is a property description"
    },
    "anInt" : {
      "type" : "integer"
    }
  }
}


回答2:

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!

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:

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.