users@jersey.java.net

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

From: rocklee <minitip_at_gmail.com>
Date: Mon, 21 Feb 2011 05:28:22 -0800 (PST)

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
-- 
View this message in context: http://jersey.576304.n2.nabble.com/can-not-unmarshal-nested-generic-list-in-a-wrapper-tp5959000p6048691.html
Sent from the Jersey mailing list archive at Nabble.com.