18.2. Using JAXB with the Atom Provider
The
org.jboss.resteasy.plugins.providers.atom.Content
class lets you marshal and unmarshal JAXB-annotated objects that form the body of an entry's content. The following code is an example of sending an Entry
with a Customer
object attached as the body of the entry's content.
@XmlRootElement(namespace = "http://jboss.org/Customer") @XmlAccessorType(XmlAccessType.FIELD) public class Customer { @XmlElement private String name; public Customer() { } public Customer(String name) { this.name = name; } public String getName() { return name; } } @Path("atom") public static class AtomServer { @GET @Path("entry") @Produces("application/atom+xml") public Entry getEntry() { Entry entry = new Entry(); entry.setTitle("Hello World"); Content content = new Content(); content.setJAXBObject(new Customer("bill")); entry.setContent(content); return entry; } }
The
Content.setJAXBObject()
method tells the content object that you are returning a Java JAXB object to be marshaled. If you use a base format other than XML (for example, application/atom+json
), the attached JAXB object will be marshaled into that format.
If your input is an Atom document, you can also extract JAXB objects from
Content
by using Content.getJAXBObject(Class class)
. The code that follows is an example of extracting a Customer
object from the Content
:
@Path("atom") public static class AtomServer { @PUT @Path("entry") @Produces("application/atom+xml") public void putCustomer(Entry entry) { Content content = entry.getContent(); Customer cust = content.getJAXBObject(Customer.class); } }