users@jersey.java.net

Re: [Jersey] Unmarshaling JAXB object from JSON

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Wed, 01 Apr 2009 14:13:53 +0200

On Apr 1, 2009, at 2:06 PM, Paul Sandoz wrote:

>
> On Apr 1, 2009, at 1:57 PM, Farrukh Najmi wrote:
>> Hi Paul,
>>
>> If the jsonString represents List<PostalAddressType> where
>> PostalAddressType is my JAXB generated bean then what should c be
>> and how can I create an instance. Sorry for asking such a basic
>> question. Also I note that the second arg to getMessageBodyReader
>> is of a different type. I can get that with following code (thanks
>> Marc):
>>
>> GenericEntity<List<PostalAddressType>> entityPostalAddresses =
>> new
>> GenericEntity<List<PostalAddressType>>(listPostalAddresses) {};
>> Type genericTypePostalAddresses = entityPostalAddresses.getType();
>>
>> So my call looks like:
>>
>> providers.getMessageBodyReader(??, genericTypePostalAddresses,
>> (java.lang.annotation.Annotation[])null, "application/json");
>>
>> and I am unsure how to get the first param in the call.
>>
>
> ?? = entityPostalAddresses.getRawType();
>

Also the second parameter should be:

   entityPostalAddresses.getType()


Alternatively it is much easier if you use the array class instead e.g.

Class<PostalAddressType[]> c = PostalAddressType[].class;

MessageBodyReader<PostalAddressType[]> r = p.getMessageBodyReader(
      c, c, new Annotation[],
      MediaType.APPLICATION_JSON_TYPE);

  PostalAddressType[] b = r.readFrom(c, c, new Annotation[],
      MediaType.APPLICATION_JSON_TYPE,
      new InBoundHeaders(),
      new ByteArrayInputStream(jsonString.getBytes());

Paul.