3

I have a entity class looks like this.

@XmlRootElement
public class ImageSuffix {

    @XmlAttribute
    private boolean canRead;

    @XmlAttribute
    private boolean canWrite;

    @XmlValue;
    private String value;
}

And I'm using following dependency for JSON generation.

<dependency>
  <groupId>com.fasterxml.jackson.jaxrs</groupId>
  <artifactId>jackson-jaxrs-json-provider</artifactId>
  <version>2.1.4</version>
</dependency>

When I tried with following code, (which referred from Generating JSON Schemas with Jackson)

@Path("/imageSuffix.jsd")
public class ImageSuffixJsdResource {

    @GET
    @Produces({MediaType.APPLICATION_JSON})
    public String read() throws JsonMappingException {

        final ObjectMapper objectMapper = new ObjectMapper();

        final JsonSchema jsonSchema =
            objectMapper.generateJsonSchema(ImageSuffix.class);

        final String jsonSchemaString = jsonSchema.toString();

        return jsonSchemaString;
    }
}

Server complains with following error message

java.lang.IllegalArgumentException: Class com.googlecode.jinahya.test.ImageSuffix would not be serialized as a JSON object and therefore has no schema
        at org.codehaus.jackson.map.ser.StdSerializerProvider.generateJsonSchema(StdSerializerProvider.java:299)
        at org.codehaus.jackson.map.ObjectMapper.generateJsonSchema(ObjectMapper.java:2527)
        at org.codehaus.jackson.map.ObjectMapper.generateJsonSchema(ObjectMapper.java:2513)

How can I fix this?

jruizaranguren
  • 12,679
  • 7
  • 55
  • 73
Jin Kwon
  • 20,295
  • 14
  • 115
  • 184
  • 1
    FYI - We are currently adding this support to MOXy's JSON-binding. You can track this work using the following link: http://bugs.eclipse.org/404452 – bdoughan Apr 10 '13 at 11:24

2 Answers2

6

Have you tried configuring your ObjectMapper to include jaxb introspector? We use spring mvc3 for implementing REST services and use the same model objects to serialize into xml/json.

AnnotationIntrospector introspector = 
    new Pair(new JaxbAnnotationIntrospector(), new JacksonAnnotationIntrospector());
objectMapper.setAnnotationIntrospector(introspector);
objectMapper.generateJsonSchema(ImageSuffix.class);

EDIT: Here is the output I get from jackson:

{
  "type" : "object",
  "properties" : {
    "canRead" : {
      "type" : "boolean",
      "required" : true
    },
    "canWrite" : {
      "type" : "boolean",
      "required" : true
    },
    "value" : {
      "type" : "string"
    }
  }
}

Hope this helps!

Ajay
  • 977
  • 2
  • 11
  • 23
  • Thanks, this helped a lot! Btw, a small edit is made to your answer. – asgs Jun 18 '13 at 20:19
  • 1
    I believe this is actually incorrect JSON Schema. The correct output should not include 'required', and should show the third type as 'type: ["string","null"]' (i.e. a nullable type). Can Jackson be configured to produce this? – Richard Kennard Aug 16 '13 at 23:06
  • @asgs I guess a lot of changes have been done to `Jackson` till now and the things have been deprecated so this answer does not work for the current version of the `Jackson`. If possible can you please let me know how can it be done using the latest version of the `Jackson`? I have `JAXB/Moxy` annotated class from which I would like to generate `JSONSchema`. – BATMAN_2008 Jun 02 '21 at 13:27
1

The provided answer is a bit old and some of the things have been deprecated now. So try the following code with the latest Jackson and JAXB/Moxy annotated classes:

Approach-1

class JsonSchemaGenerator{

  public static void main(String[] args) throws JsonProcessingException {
    ObjectMapper objectMapper = new ObjectMapper();
    TypeFactory typeFactory = TypeFactory.defaultInstance();
    AnnotationIntrospector introspector = new JaxbAnnotationIntrospector(typeFactory);
    objectMapper.getDeserializationConfig().with(introspector);
    objectMapper.getSerializationConfig().with(introspector);

    //To force mapper to include JAXB annotated properties in Json schema
    objectMapper.registerModule(new JaxbAnnotationModule());
    SchemaFactoryWrapper visitor = new SchemaFactoryWrapper();
    objectMapper.acceptJsonFormatVisitor(objectMapper.constructType(Customer.class), visitor);

    JsonSchema inputSchema = visitor.finalSchema();
    String schemaString = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(inputSchema);

    System.out.println(schemaString);
  }

}

Approach -2 :

class JsonSchemaGenerator{
  public static void main(String[] args) throws JsonProcessingException, ClassNotFoundException {
    final ObjectMapper mapper = new ObjectMapper();
    final TypeFactory typeFactory = TypeFactory.defaultInstance();
    final AnnotationIntrospector introspector = new JaxbAnnotationIntrospector(typeFactory);
    mapper.getDeserializationConfig().with(introspector);
    mapper.getSerializationConfig().with(introspector);
    final JsonSchema jsonSchema = mapper.generateJsonSchema(Class.forName("com.jaxb.Customer"));
    System.out.println(jsonSchema);
  }
}
BATMAN_2008
  • 2,788
  • 3
  • 31
  • 98