users@jersey.java.net

[Jersey] Re: can not unmarshal nested generic list in a wrapper

From: Jakub Podlesak <jakub.podlesak_at_oracle.com>
Date: Mon, 21 Feb 2011 17:25:43 +0100

Hi Lee,

Try to go with 3, where XML format is fine
and unmarshalling works for you.

Then for JSON, just enable the POJO_MAPPING [1]
feature, which makes JSON processing use
the Jackson providers. They work directly
with the Java model as opposed to working
with the XML info set.

HTH

~Jakub

[1]http://jersey.java.net/nonav/documentation/latest/json.html#d4e878

On 02/21/2011 02:28 PM, rocklee wrote:
> Hi Pavel/Adam/Damiano,
>
> Thanks a lot for recommendations. I made some changes to the code and almost
> get there. I tried different combination of annotations to the
> GenericResponse class, sometimes the format of generated xml or json is
> good, but can not be unmarshalled, sometimes the response xml or json can be
> unmarshalled, but the format is not what i want.
>
> The format I want is as following:
>
> Json Format:
> {"page":1,"limit":10,"total":150,"items":[{"id":0,"username":"username
> 0","firstName":"firstName 0","lastName":null,"role":{"name":"General
> User","id":1}}]}
>
> Xml Format:
> <?xml version="1.0" encoding="UTF-8"
> standalone="yes"?><response><page>1</page><limit>10</limit><total>150</total><items><user><id>0</id><username>username
> 0</username><firstName>firstName 0</firstName><lastName
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xsi:nil="true"/><role><id>1</id><name>General
> User</name></role></user></items></response>
>
> The following are what I tried and the result.
>
> 1) No annotation for items
> Format: json format is good, xml format contains no wrapper, "items" becomes
> element of sub types, contains xsi:type attribute.
> Unmarshal: both json and xml works well
>
> Json Format:
> {"page":1,"limit":10,"total":150,"items":[{"id":0,"username":"username
> 0","firstName":"firstName 0","lastName":null,"role":{"name":"General
> User","id":1}}]}
>
> Xml Format:
> <?xml version="1.0" encoding="UTF-8"
> standalone="yes"?><response><page>1</page><limit>10</limit><total>150</total><items
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xsi:type="user"><id>0</id><username>username
> 0</username><firstName>firstName 0</firstName><lastName
> xsi:nil="true"/><role><id>1</id><name>General
> User</name></role></items></response>
>
> 2) Format: both xml and json format is good,
> Unmarshal: json format works well, xml format not
>
> @XmlAnyElement
> @XmlElementWrapper(name = "items")
>
> Json Format:
> {"page":1,"limit":10,"total":150,"items":[{"id":0,"username":"username
> 0","firstName":"firstName 0","lastName":null,"role":{"name":"General
> User","id":1}}]}
>
> Xml Format:
> <?xml version="1.0" encoding="UTF-8"
> standalone="yes"?><response><page>1</page><limit>10</limit><total>150</total><items><user><id>0</id><username>username
> 0</username><firstName>firstName 0</firstName><lastName
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xsi:nil="true"/><role><id>1</id><name>General
> User</name></role></user></items></response>
>
> 3) Format: json format contains root wrapper of sub type, xml format is
> good.
> Unmarshal: both xml and json works well
>
> @XmlElementWrapper(name = "items")
> @XmlElements({
> @XmlElement(name="user", type=User.class),
> @XmlElement(name="product", type=Product.class)
> })
>
> Json Format:
> {"page":1,"limit":10,"total":150,"items":[{"user":{"id":0,"username":"username
> 0","firstName":"firstName 0","lastName":null,"role":{"name":"General
> User","id":1}}}]}
>
> Xml Format:
> <?xml version="1.0" encoding="UTF-8"
> standalone="yes"?><response><page>1</page><limit>10</limit><total>150</total><items><user><id>0</id><username>username
> 0</username><firstName>firstName 0</firstName><lastName
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xsi:nil="true"/><role><id>1</id><name>General
> User</name></role></user></items></response>
>
> 4) Format: json format contains root wrapper of sub type, xml format
> contains no "items" wrapper.
> Unmarshal: both xml and json works well
>
> @XmlElements({
> @XmlElement(name="user", type=User.class),
> @XmlElement(name="product", type=Product.class)
> })
>
> Json Format:
> {"page":1,"limit":10,"total":150,"items":[{"user":{"id":0,"username":"username
> 0","firstName":"firstName 0","lastName":null,"role":{"name":"General
> User","id":1}}}]}
>
> Xml Format:
> <?xml version="1.0" encoding="UTF-8"
> standalone="yes"?><response><page>1</page><limit>10</limit><total>150</total><user><id>0</id><username>username
> 0</username><firstName>firstName 0</firstName><lastName
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xsi:nil="true"/><role><id>1</id><name>General
> User</name></role></user></user></response>
>
> 5) Format: json format is good, "items" becomes wrapper element and sub
> element
> Unmarshal: both xml and json works well
>
> @XmlElementWrapper(name = "items")
>
> Json Format:
> {"page":1,"limit":10,"total":150,"items":[{"id":0,"username":"username
> 0","firstName":"firstName 0","lastName":null,"role":{"name":"General
> User","id":1}}]}
>
> Xml Format:
> <?xml version="1.0" encoding="UTF-8"
> standalone="yes"?><response><page>1</page><limit>10</limit><total>150</total><items><items
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xsi:type="user"><id>0</id><username>username
> 0</username><firstName>firstName 0</firstName><lastName
> xsi:nil="true"/><role><id>1</id><name>General
> User</name></role></items></items></response>
>
> Please help me, any suggestions will be appreciated.
>
> Best Regards,
> Lee