It should work with a change to your genericType parameters for
getMessageBodyReader and readFrom. By passing in the MyClass type, you're
telling the Jackson provider that you want to deserialize the array into a
MyClass Object. Instead you need to pass in the type of the collection
that you want to deserialize to. This can be accomplished using the
GenericEntity class.
Here's an example code snippet:
GenericEntity<List<Person>> genericEntity = new
GenericEntity<List<Person>>(new ArrayList<Person>()) {};
MessageBodyReader<Collection> reader = providers
.getMessageBodyReader(Collection.class, genericEntity.getType(), null,
MediaType.APPLICATION_JSON_TYPE);
Use the same genericEntity.getType() call for the readFrom method. Hope
this helps!
-- Jesse
From: Mike Rheinheimer <[email protected]>
To: [email protected]
Date: 11/15/2010 08:53 AM
Subject: Re: Invoking MessageBodyReader.readFrom for Collection
Hi Dave,
I replied on the Jackson list where you asked the same question, but I'll
reply here too for the benefit of the community:
There is a limitation in Jackson 1.5.5 especially regarding how it handles
Collection types. Please try updating to Jackson 1.5.8 or later and see
if that fixes the problem.
Hopefully unrelated, but needs to be asked: are you using JAXB objects?
Any @XmlJavaTypeAdapters? If you don't know, then the answer is 'no'. :)
Thanks!
mike
On Mon, Nov 15, 2010 at 7:40 AM, David W Palmieri <[email protected]>
wrote:
I cannot seem to figure out how to invoke MessageBodyReader.readFrom() for
a collection of objects.
I have not implemented my own MessageBodyReader. Rather, I am trying to
invoke the "standard" MessageBodyReader to read a collection of objects
from the input stream. The objects are of a class that I have defined
using standard getters/setters. I have confirmed that I can use the
"standard" MessageBodyReader to read a single instance of my class of
object. But when I try to use the "standard" MessageBodyReader to read a
collection of them, it fails.
Here is a snippet of code showing my attempt:
@Context private Providers providers;
@Context private HttpHeaders headers;
@Context private HttpServletRequest request;
MessageBodyReader<Collection> messageBodyReader =
providers.getMessageBodyReader(
Collection.class,
MyClass.class,
null,
MediaType.APPLICATION_JSON_TYPE);
try {
Collection<MyClass> myObjects = messageBodyReader.readFrom(
Collection.class,
MyClass.class,
null,
MediaType.APPLICATION_JSON_TYPE,
headers.getRequestHeaders(),
request.getInputStream());
} catch (IOException e) {
}
When I execute the code, I get the following IOException:
org.codehaus.jackson.map.JsonMappingException: Can not deserialize
instance of com.mycompany.MyClass out of START_ARRAY token
at [Source: com.ibm.ws.webcontainer.srt.http.httpinputstr...@34643464;
line: 1, column: 1]
>From the exception, it seems that it is trying to read it as a single
instance of MyClass, rather than a Collection.
Can anyone provide any advice or guidance?
Regards,
Dave