users@jersey.java.net

[Jersey] Re: JSON / XML unmarshaller problem

From: Pengfei Di <pengfei.di_at_match2blue.com>
Date: Wed, 13 Apr 2011 16:54:10 +0200

Hi Jairo,

if different reponse types are possible, you may use the http header to
parse your ClientResponse.

ClientResponse responseJ = server.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
//Get the entity of the response
if (responseJ..getStatus() = 200)
        MyClass v = responseJ.getEntity(MyClass.class);
else
        MyClass2 u = responseJ.getEntity(MyClass2.class);


Pengfei

On 04/13/2011 04:37 PM, Jakub Podlesak wrote:
> Hi Jairo,
>
> your use case seems a bit overcomplicated.
>
> Please see [1] on how to configure Jackson based POJO JSON support
> in Jersey (both server and client side should be covered).
>
> Then all you need to do on the client side is:
>
> MyClass v =
> server.accept(MediaType.APPLICATION_JSON).get(MyClass.class);
>
>
> see also [2] for an example.
>
> Does it help?
>
> ~Jakub
>
> [1]http://jersey.java.net/nonav/documentation/latest/json.html#d4e894
> [2]http://download.java.net/maven/2/com/sun/jersey/samples/jacksonjsonprovider/1.6/jacksonjsonprovider-1.6-project.zip
>
>
>
> On 04/13/2011 05:50 PM, Jairo1 wrote:
>> Hi,
>> I'm facing with the following issue:
>>
>> I'm trying to unmarshal the JSON data received from web server using this
>> approach:
>>
>> ****************
>> ClientResponse responseJ =
>> server.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
>> //Get the entity of the response
>> String textEntityJ = (String)responseJ.getEntity(String.class);
>>
>> JSONJAXBContext contextJson = null;
>> JSONUnmarshaller unmarshallerJson = null;
>> MyClass myClassJson = null;
>>
>> //built JSONConfiguration based on mapped notation
>> contextJson = new JSONJAXBContext(JSONConfiguration.natural().build(),
>> MyClass.class);
>> unmarshallerJson = contextJson.createJSONUnmarshaller();//create JSON
>> Unmarshaller
>>
>> myClassJson = unmarshallerJson.unmarshalFromJSON(new
>> StringReader(textEntityJ), MyClass.class);
>> ***********
>>
>> Even I get the data in the 'textEntityJ' in the format
>> {"version":["abb.123","xyz.456"]} the unmarshaller returns always MyClass
>> object with empty List ( myClassJson.getVersion().size = 0).
>>
>> MyClass structure:
>>
>>
>> @XmlAccessorType(XmlAccessType.FIELD)
>> @XmlType(name = "", propOrder = {
>> "version"
>> })
>> @XmlRootElement(name = "versions")
>> public class MyClass {
>>
>> protected List version;
>>
>>
>> public List getVersion() {
>> if (version == null) {
>> version = new ArrayList();
>> }
>> return this.version;
>> }
>>
>> }
>>
>>
>> What could be wrong? The same issue I'm facing with XML approach. I receive
>> data (as String) in the XML format, but unmarshaller failes in case when
>> Java bean contains LIST object.
>>
>> Thanks in advance for your help,
>> Jairo
>>
>> --
>> View this message in context: http://jersey.576304.n2.nabble.com/JSON-XML-unmarshaller-problem-tp6269059p6269059.html
>> Sent from the Jersey mailing list archive at Nabble.com.
>>


-- 
Pengfei Di
Technology
match2blue software development GmbH
Leutragraben 1
07743 Jena
Tel: +49 3641 816 8092
Mobil: +49 1520 166 8691
Fax: +49 3641 573 3479
Email: pengfei.di_at_match2blue.com
Web  : www.match2blue.com
Blog : http://blog.match2blue.com
Registergericht: Amtsgericht Jena
Registernummer: HRB 503726
Geschäftsführerin: Stephanie Renda