users@jersey.java.net

Re: [Jersey] Unmarshaling JAXB object from JSON

From: Marc Hadley <Marc.Hadley_at_Sun.COM>
Date: Tue, 31 Mar 2009 15:25:54 -0400

On Mar 31, 2009, at 2:24 PM, Farrukh Najmi wrote:

> Farrukh Najmi wrote:
>> Farrukh Najmi wrote:
>>> Marc Hadley wrote:
>>>> I think if you inject an instance of Providers then you should be
>>>> able to use the getMessageBodyReader method to get a reader that
>>>> will unmarshall your JSON string.
>>>>
>>>
>>> Thanks Marc. Can you tell me how to "inject an instance of
>>> Providers". I have a JAXBContextResolver class that implements
>>> ContextResolver<JAXBContext>. I do not know though how I can get
>>> an instance of that class injected in my class.
>>>
>> OK. I RTFMed of the jsr 311 spec.
>>
>> I can get Providers injected via @Context annotation within my
>> class. Thanks.
>>
>
> I am unclear from javadoc of getMessageBodyWriter what I should use
> for type and genericType params.
> I need to unmarshal the JSON String into List<PostalAddressType>.
>
Type would be List.class, working with generic Type can be awkward,
but you should be able to do something like:

List<PostalAddressType> list = new ArrayList<PostalAddressType>();
GenericEntity<List<PostalAddressType>> entity = new
GenericEntity<List<PostalAddressType>>(list) {};
Type genericType = entity.getType();

There may be a simpler way to do that, e.g. if you have a class field
of type List<PostalAddressType> you can use the reflection APIs to get
the generic type directly.

> Also, I am not sure if I need to supply annotations param.
>
null or an empty array should work fine for your purposes. I just
noticed that we don't specify whether null is allowed so we'll need to
fix that in a maintenance release.

Marc.