users@jersey.java.net

Re: [Jersey] serialization mistery - part I

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Tue, 05 May 2009 22:30:57 +0200

On May 5, 2009, at 7:00 PM, Felipe Gaścho wrote:

> 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
>

The result will be the same if Object is returned. The return type of
the method is not utilized.

JAXB message body writer calls:

    Marshaller.marshal(Object, OutputStream)
    http://java.sun.com/javase/6/docs/api/javax/xml/bind/Marshaller.html#marshal(java.lang.Object,%20java.io.OutputStream)


> ------------ 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
>

The result will be the same if Object is returned. The return type of
the method is not utilized.

JAXB message body writer calls:

    Marshaller.marshal(Object, OutputStream)
    http://java.sun.com/javase/6/docs/api/javax/xml/bind/Marshaller.html#marshal(java.lang.Object,%20java.io.OutputStream)


> ------------ 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
>

The only way i can explain this is if the items in the collection are
concrete instances of FpAbstractUser.

The JAXB message body writer creates a Marshaller, configures it to
write fragments and then calls:

     public final void writeList(Class<?> elementType, Collection<?> t,
             MediaType mediaType, Charset c,
             Marshaller m, OutputStream entityStream)
             throws JAXBException, IOException {
         final String rootElement = getRootElementName(elementType);
         final String cName = c.name();

         entityStream.write(
                 String.format("<?xml version=\"1.0\" encoding=\"%s\"
standalone=\"yes\"?>", cName).getBytes(cName));
         entityStream.write(String.format("<%s>",
rootElement).getBytes(cName));

         for (Object o : t)
             m.marshal(o, entityStream); <------- Uses the same
marshal method as 1 & 2 for each item of the collection

         entityStream.write(String.format("</%s>",
rootElement).getBytes(cName));
     }

Paul.

>
> 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
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>