My question is as follows:
jackson desearlization: Two keys at root. How do i unwrap one and ignore the other?
I want this problem to be solved without using a wrapper class as solved in this question.
I am developing a jersey client application. I've configured a jackson data bind
integrated with jersey.
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);
a minxin interface:
@JsonRootName("items")
interface CollectionMixIn {
}
the server returns a json response like this:
{"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"}]}
I read the above json by the help of jersey client:
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);
Note that PicsDetail
is a POJO of mine. I want to read a collection of PicsDetail
s. but I face this exception:
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)
the problem is that,
Root name 'totalCount' does not match expected ('items')
the response json contains items
property, but jackson has not sense of it.Please help!
similar questions are resolved by a wrapper class.
I don't want to use a wrapper class like this:
class Wrapper{
int totalCount;
Set items;
}
similar questions: