我有一個實體類看起來像這樣。如何從JAXB註釋類生成JSON模式?
@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綁定中。您可以使用以下鏈接跟蹤這項工作:http://bugs.eclipse.org/404452 – 2013-04-10 11:24:05