users@jersey.java.net

[Jersey] JSON serialization anomaly

From: <julian.triggs_at_astrazeneca.com>
Date: Tue, 10 Apr 2012 09:36:10 +0000 (GMT)

I have a resource method that can produce either xml or json. The xml
that gets created is as expected but the json serialization is not.
When returning Json the UserMappingArray wrapper is not serialized,
instead only an array of UserMapping is serialized. On the client if I
try to getEntity() of type UserMappingArray an object of that type is
returned but its empty as one might expect as that object type didn’t
appear in the json. Incidentally if I try to getEntity() with type
UserMapping[].class that doesn’t work - causing an exception although I
wouldn't expect to have to do that anyway as the serialization format
should be handled transparently. I’m using jersey 1.12

Any advice?

The resource interface is:-
@GET
@Path(URIPART_MOSAIC_USERS)
@Produces([mediaType])
Response findMosaicUsers(//...

Where [mediaType] is MediaType.APPLICATION_JSON or
MediaType.APPLICATION_XML

The object being placed in the response is:-

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "UserMappingArray", propOrder = {
    "userMappings"
})
@XmlRootElement(name = "userMappingArray")
public class UserMappingArray {
    @XmlElement(name = "userMapping")
    protected List<UserMapping> userMappings;

and:-

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "UserMapping", propOrder = {
    "mosaicId",
    "userName"
})
@XmlRootElement(name = "userMapping")
public class UserMapping{
    protected int mosaicId;
    @XmlElement(required = true)
    protected String userName;

(created via xjc from a standard venetian blind schema with custom
bindings to force XmlRootElement to be created for every type)

Example xml returned via curl:-
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<userMappingArray xmlns="urn:foofoo">
<userMapping><mosaicId>51</mosaicId>....

Json:-
{"userMapping":[{"mosaicId":"51","userName":....