users@jersey.java.net

XmlJavaTypeAdapter and jersey

From: Christopher Piggott <cpiggott_at_gmail.com>
Date: Tue, 10 Aug 2010 15:01:11 -0400

Hi,

I understand about messagebodywriters but I'm trying to figure out a way for
this to happen more automatically. Here's the situation:

I have an object that's not a true POJO in the sense that it has no default
constructor and it's immutable, e.g.:

public class ImmutableSausage {
   final int bites;
   public ImmutableSausage(int numBites) { bites = numBites; }
   public int getBites() { return bites; }
}

I haven't totally figured out this works, but I gather that to serialize
this I make:

@XmlElement
@XmlJavaTypeAdapter(SausageAdapter.class)
public class SausageBean {
   int bites;
   public void setBites( int numBites) { bites = numBites; }
   @XmlAttribute
   public int getBites() { return bites; }
}

then I create a SausageAdapter implements XmlAdapter<SausageBean,
ImmutableSausage>

(this is where I get to the jersey part)

@GET
@Produces("application/xml")
ImmutableSausage getSausage()
{
   return new ImmutableSausage(12);
}


so, my resource method returns something that is not at all a bean, but 1.
jaxb knows what to do with it because of the XmlJavaTypeAdapter 2. it's all
handled by jaxb so that I don't need MessageBodyReaders or
MessageBodyWriters

Do I understand this correctly, and should it work?

--C