【问题标题】:How to generate JSON schema from a JAXB annotated class?如何从 JAXB 注释类生成 JSON 模式?
【发布时间】:2013-04-10 02:38:01
【问题描述】:

我有一个像这样的实体类。

@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)

我该如何解决这个问题?

【问题讨论】:

  • 仅供参考 - 我们目前正在将此支持添加到 MOXy 的 JSON 绑定中。您可以使用以下链接跟踪这项工作:bugs.eclipse.org/404452

标签: json jaxb jackson jsonschema


【解决方案1】:

您是否尝试过配置您的 ObjectMapper 以包含 jaxb 内省?我们使用 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"
    }
  }
}

希望这会有所帮助!

【讨论】:

  • 谢谢,这帮助很大!顺便说一句,对您的答案进行了小修改。
  • 我相信这实际上是不正确的 JSON Schema。正确的输出不应包含“必需”,并且应将第三种类型显示为“类型:[“字符串”,“空”]”(即可为空的类型)。可以配置杰克逊来制作这个吗?
  • @asgs 我想到目前为止Jackson 已经进行了很多更改,并且这些内容已被弃用,因此此答案不适用于Jackson 的当前版本。如果可能的话,请让我知道如何使用最新版本的Jackson 来完成它?我有 JAXB/Moxy 带注释的类,我想从中生成 JSONSchema
【解决方案2】:

提供的答案有点陈旧,有些东西现在已被弃用。所以用最新的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);
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多