Jin*_*won 3 json jaxb jsonschema jackson
我有一个实体类看起来像这样。
@XmlRootElement
public class ImageSuffix {
@XmlAttribute
private boolean canRead;
@XmlAttribute
private boolean canWrite;
@XmlValue;
private String value;
}
Run Code Online (Sandbox Code Playgroud)
我正在使用以下依赖项来生成 JSON。
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.1.4</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
当我尝试使用以下代码时,(引用自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;
}
}
Run Code Online (Sandbox Code Playgroud)
服务器抱怨以下错误消息
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)
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个问题?
您是否尝试过将 ObjectMapper 配置为包含 jaxb 内省器?我们使用 spring mvc3 来实现 REST 服务,并使用相同的模型对象序列化为 xml/json。
AnnotationIntrospector introspector =
new Pair(new JaxbAnnotationIntrospector(), new JacksonAnnotationIntrospector());
objectMapper.setAnnotationIntrospector(introspector);
objectMapper.generateJsonSchema(ImageSuffix.class);
Run Code Online (Sandbox Code Playgroud)
编辑:这是我从杰克逊那里得到的输出:
{
"type" : "object",
"properties" : {
"canRead" : {
"type" : "boolean",
"required" : true
},
"canWrite" : {
"type" : "boolean",
"required" : true
},
"value" : {
"type" : "string"
}
}
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!
| 归档时间: |
|
| 查看次数: |
7116 次 |
| 最近记录: |