users@jersey.java.net

[Jersey] Problems with Jersey - JAXB for generic payloads.

From: Navin Desai <ndesai_at_tagworldwide.com>
Date: Wed, 5 Jun 2013 11:00:18 +0000

Hi all,
               We are having a problem with generic Payload while using Jersey. Here is our Domain object.



@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Event<T> {
       private T eventPayload;
       private String eventType;
}

Here we have top level domain object defined. But the internal domain object is generic.

Now on the resource endpoint we have something like this as we know that the sub-domain object we were expecting is.

@POST
       @Path("log")
       @Consumes(MediaType.APPLICATION_XML)
       public Response writeLog(Event<LogPayload> event)

But this doesn't work.
The event instance is created but the subdomain is not populated correctly.
It just tries to populate the sub-domain object with any random domain object which has the same root element as in the XML (there may be more than one).

Our Solution:
This is our solution, but I am sure this is not the best.

We have to modify our parent domain object have a String variable which stores XML-representation of the generic payload in a String format. For this we have had to write our own Jaxb marshaller.
Modifications to the Event
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Event<T> {
       @XmlTransient
       private T eventPayload;
       private String eventType;
       private String payLoadXML;

// Changes to the constructor:
public Event(T eventPayload ......) {
              super();
              this.eventPayload = eventPayload;
              payLoadXML = JAXBUtils.marshall(eventPayload,false); }}


On the resource side once we get the parent Event object, we have to again use our own jaxb marshaller to get the required domain object from the payloadXML as follows.
@POST
       @Path("log")
       @Consumes(MediaType.APPLICATION_XML)
       public Response writeLog(Event<LogPayload> event)
              LogPayload log1 = (LogPayload) JAXBUtils.unMarshall(
                                         event.getPayLoadXML(),LogPayload.class);

So ineffect we are using our jaxbmarshaller to marshall and unmarshall the generic subdomain object before and after making the rest call....

Please lets us know if there is a better way to do this ?

Thanks,
ND

This e-mail has been scanned for all viruses by Star.