[英]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:因此,请使用最新的
Jackson
和JAXB/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.