users@jersey.java.net

Re: [Jersey] Unmarshaling JAXB object from JSON

From: Farrukh Najmi <farrukh_at_wellfleetsoftware.com>
Date: Wed, 01 Apr 2009 07:57:10 -0400

Paul Sandoz wrote:
>
> On Mar 31, 2009, at 6:46 PM, Farrukh Najmi wrote:
>
>> Paul Sandoz wrote:
>>>
>>> On Mar 31, 2009, at 5:48 PM, Farrukh Najmi wrote:
>>>
>>>> Paul Sandoz wrote:
>>>>>
>>>>> On Mar 31, 2009, at 5:36 PM, Farrukh Najmi wrote:
>>>>>
>>>>>>
>>>>>> Hi Paul,
>>>>>>
>>>>>> In an earlier email you had mentioned that jersey can unmarshal a
>>>>>> JAXB object from a JSON object.
>>>>>> My jersey client sends a POST message with JSON string as value
>>>>>> for a form parameter. How can I
>>>>>> take that JSON string and unmarshal it to a JAXB object. I would
>>>>>> be grateful for some focused tip on
>>>>>> how to do this unmarshalling manually. Thanks.
>>>>>>
>>>>>
>>>>> What is the media type of the form "multipart/form-data" or
>>>>> "application/x-www-form-urlencoded" ?
>>>>>
>>>>
>>>> I am not sure as I am using a 3rd party toolkit. How can I get this
>>>> info from debugging the jersey server side code?
>>>>
>>>
>>> Add a logging filter:
>>>
>>> https://jersey.dev.java.net/source/browse/*checkout*/jersey/tags/jersey-1.0.2/api/jersey/com/sun/jersey/api/container/filter/LoggingFilter.html
>>>
>>>
>>
>> LoggingFilter shows that mediatype is:
>> application/x-www-form-urlencoded
>>
>
> OK.
>
> Ideally you should be able to reuse JSONJAXBContext for marshalling
> and unmarshalling JAXB beans to and from JSON, but unfortunately this
> is not currently possible (plus there are some bound assumptions that
> need to be cleaned up before such an API is cleanly exposed).
>
> So as Marc suggests using Providers is currently the best way:
>
> String jsonString = ...
>
> Providers p = ...
>
> Class<MyJSONBean> c = ...
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.

Thanks for your help.
>
> MessageBodyReader<MyJSONBean> r = p.getMessageBodyReader(c, c, new
> Annotation[],
> MediaType.APPLICATION_JSON_TYPE);
>
> MyJSONBean b = r.readFrom(c, c, new Annotation[],
> MediaType.APPLICATION_JSON_TYPE,
> new InBoundHeaders(),
> new ByteArrayInputStream(jsonString.getBytes());
>
> Ugly i know :-( but until we clean up the JSON JAXB API to be reusable
> that is the only solution.
>
I can deal with it for now thanks to your help.

-- 
Regards,
Farrukh
Web: http://www.wellfleetsoftware.com