如何从JAXB注释类生成JSON模式?

时间:2013-04-10 02:38:02

标签: json jaxb jackson jsonschema

我有一个实体类看起来像这样。

@XmlRootElement
public class ImageSuffix {

    @XmlAttribute
    private boolean canRead;

    @XmlAttribute
    private boolean canWrite;

    @XmlValue;
    private String value;
}

我正在使用以下依赖关系进行JSON生成。

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

当我尝试使用以下代码时(从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;
    }
}

服务器抱怨以下错误消息

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)

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:4)

您是否尝试过配置ObjectMapper以包含jaxb introspector?我们使用spring mvc3来实现REST服务,并使用相同的模型对象序列化为xml / json。

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

编辑:这是我从杰克逊那里获得的输出:

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

希望这有帮助!

答案 1 :(得分:0)

提供的答案有点旧,现在已弃用某些内容。因此,请使用最新的 JacksonJAXB/Moxy 注释类尝试以下代码:

方法一

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);
  }

}

方法-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);
  }
}