users@jersey.java.net

serialization mistery - part I

From: Felipe Gaúcho <fgaucho_at_gmail.com>
Date: Tue, 5 May 2009 19:00:50 +0200

I am still having an unexpected behavior from the Jersey serialization
of entities in XML (or JSON, the format doesn't matter):

I have a "split entity", what means a table mapped by three Java classes:

1) @MappedSuperclass AbstractFootprintEntity implements Serializable
2) @Entity FpAbstractUser extends AbstractFootprintEntity
3) @Entity FpUser extends FpAbstractUser

That hierarchy works and all my JPA operations works fine... no JPA
problem at all....

Now I am exposing these classes through a Jersey service interface,
and then I have the mysterious behavior:

------------ code 1:

        @GET
        @Produces( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
        @Path("/read/{id}")
        public JAXBElement<FpAbstractUser> read(@PathParam("id") String id) {
                return new JAXBElement<FpAbstractUser>(
                                FpAbstractUser.QUALIFIED_NAME, FpAbstractUser.class,
                                userFacade.read(Long.valueOf(id)));
        }

returns: an XML with an object of type FpUser << ?? why not
FpAbstractUser as expected ?
example: http://fgaucho.dyndns.org:8080/footprint-service/user/read/2

------------ code 2:

        @GET
        @Produces( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
        @Path("/edit/{id}")
        public FpUser edit(@PathParam("id") String id) {
                return userWritableFacade.read(new Long(id));
        }

returns: an XML with an object of type FpUser << OK, that's exactly
what I expected
example: http://fgaucho.dyndns.org:8080/footprint-service/user/edit/2

------------ code 3:

        @GET
        @Produces( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
        @Path("/readall/{offset}/{limit}")
        public Collection<FpAbstractUser> read(@PathParam("offset") int offset,
                @PathParam("limit") int limit) {
                return userFacade.readAll(offset, limit);
        }

return: an XML with a collection of FpAbstractUser << OK, that's
exactly what I expected
example: http://fgaucho.dyndns.org:8080/footprint-service/user/readall/0/99


So, how can I return an XML of a serialized FpAbstractUser (only one,
not the collection...)

what is the difference between serializing one object referenced by
its superclass and a collection of objects referenced by their
superclasses? somehow the result is different...

* I tried to use Object as return type but it didn't solved the problem.....





-- 
Please help to test this application:
http://fgaucho.dyndns.org:8080/cejug-classifieds-richfaces