JsonMappingException:根名称与预期不匹配

时间:2015-02-05 13:44:51

标签: java json jersey jackson jersey-client

我的问题如下:
jackson desearlization:根目录下的两个键。我如何打开一个而忽略另一个?
我希望在不使用this question中解决的包装类的情况下解决此问题 我正在开发一个球衣客户端应用程序。我配置了一个jackson数据绑定 与球衣整合。

Map<Class<?>, Class<?>> mixins = new HashMap<Class<?>, Class<?>>();
    mixins.put(Set.class, CollectionMixIn.class);
    return new ObjectMapper()
            .configure(SerializationFeature.WRAP_ROOT_VALUE, true)
            .configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true)
            .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false)
            .setMixIns(mixins);

minxin接口:

@JsonRootName("items")
interface CollectionMixIn {
}

服务器返回如下的json响应:

{"totalCount":6,"items":[{"id":62,"lat":30.2173,"lon":50.186405,"alt":0.0,"imageFileName":"DSC_0410 - Copy (3).JPG","imageFileSize":7671969,"imageFileSizeAsString":"7.32 MB"},{"id":65,"lat":30.2173,"lon":50.186405,"alt":0.0,"imageFileName":"DSC_0410 - Copy.JPG","imageFileSize":7671969,"imageFileSizeAsString":"7.32 MB"}]}

我在泽西客户的帮助下阅读了上述json:

 WebTarget target = webTarget.path("getPics");
 target = target.queryParam("nodeId", 67);
 Invocation.Builder builder =  target.request(MediaType.APPLICATION_JSON_TYPE);
 Set<PicsDetail> res = builder.get(Set.class);

请注意PicsDetail是我的POJO。我想阅读PicsDetail的集合。但是我遇到了这个例外:

 Exception in thread "main" javax.ws.rs.ProcessingException: Error reading entity from input stream.
   at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:866)
   at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:783)
 ....
 ....
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Root name 'totalCount' does not match expected ('items') for type [collection type; class java.util.Set, contains [simple type, class java.lang.Object]]
   at [Source: org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream@4f67eb2a; line: 1, column: 2]
   at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)

问题是,

Root name 'totalCount' does not match expected ('items')

响应json包含items属性,但杰克逊没有意识到它。请帮忙! 类似的问题由包装类解决。 我不想使用这样的包装类:

class Wrapper{
   int totalCount;
   Set items;
}

类似的问题:

0 个答案:

没有答案