简体   繁体   English

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

[英]How to generate JSON schema from a JAXB annotated class?

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.我正在使用以下依赖项来生成 JSON。

<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 )当我尝试使用以下代码时,(引用自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?我怎样才能解决这个问题?

Have you tried configuring your ObjectMapper to include jaxb introspector?您是否尝试过将 ObjectMapper 配置为包含 jaxb 内省器? We use spring mvc3 for implementing REST services and use the same model objects to serialize into xml/json.我们使用 spring mvc3 来实现 REST 服务,并使用相同的模型对象序列化为 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!希望这可以帮助!

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:因此,请使用最新的JacksonJAXB/Moxy注释类尝试以下代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为 JAXB/Moxy 注释的 POJO class 生成 JSONSchema - How to generate the JSONSchema for the JAXB/Moxy annotated POJO class RestEasy,如何防止将JAXB注释的POJO与JSON封送 - RestEasy, how to prevent marshalling of JAXB annotated POJOs to and from JSON 从 Java 类生成 JSON 模式 - Generate JSON schema from Java class 使用Jersey和Jackson从JSON解组带JAXB注释的类 - Unmarshal JAXB-Annotated Classes from JSON using Jersey and Jackson 从XSD XML模式(使用JAXB)生成示例JSON文档的快速简便的方法是什么? - What's a quick and simple way to generate a sample JSON document from an XSD XML schema (using JAXB)? 如何从 yang 模块生成 JSON 模式? - How to generate JSON schema from yang module? 如何从python dict生成json模式 - How to generate json schema from python dict 当我只得到一个字符串时,如何从带有泛型的Java类生成JSON模式? - How can I generate a JSON Schema from a Java class with generics when I am only given a string? 如何在xcode(swift)中从Json Schema / Json生成模型对象? - How to generate model objects from Json Schema / Json in xcode (swift)? 如何从json模式生成简单的JSON对象? - How to generate simple JSON object from json schema?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM