Friday, February 21, 2014

Jersey REST retrieve List of custom object

It took me while to understand how one require to  retrieve generic object from ClientResponse.

I had an array of objects in Json format as a response of an REST api, which I was trying to deserialize it into List<CustomObject> refer.

	ClientResponse rsp = webresource.path(id.toString()).type(MediaType.APPLICATION_JSON).get(ClientResponse.class);
	List<CustomObject> list = rsp.getEntity(new GenericType<List<CustomObject>>(CustomObject.class));


But I kept on getting following Error:

Caused by: org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of test.CustomObject out of START_ARRAY token
 at [Source: [email protected]b1e6; line: 1, column: 1]
	at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163)


Issue got solved with following way


	ClientResponse rsp = webresource.path(id.toString()).type(MediaType.APPLICATION_JSON).get(ClientResponse.class);
	List<CustomObject> list = rsp.getEntity(new GenericType<List<CustomObject>>(){});


5 comments:

  1. Thanks very much!

    ReplyDelete
  2. Is your group playing your sibling's group? Bring your canine along as good help and ensure he has his most loved jersey on.
    Greg Bird jersey

    ReplyDelete