users@jersey.java.net

Re: [Jersey] returning JAXB type from a REST method

From: Marc Hadley <Marc.Hadley_at_Sun.COM>
Date: Mon, 12 May 2008 11:28:16 -0400

You should be able to return JAXBElement<RowsType> instead. The
problem is that RowsType doesn't have an @XmlRootElement annotation so
Jersey doesn't know what element name to use for the instance of the
schema type. Another alternative is to add a global element
declaration to the schema for an element of the RowsType schema type,
JAXB will then generate a class annotated with @XmlRootElement that
you can return directly.

Marc.


On May 12, 2008, at 11:26 AM, Arul Dhesiaseelan wrote:

> Hello,
>
> I am implementing a Rest service using Jersey which uses Java
> classes generated from JAXB. My method returns a JAXB type
> 'RowsType' which is a list of 'RowType' elements. My method looks
> like:
>
> @GET
> @Path("/list")
> @ProduceMime({"application/json", "application/xml"})
> public RowsType getRows() throws IOException {
> final RowsType list;
> list = processRows();
> return list;
> }
>
>
> private RowsType processRows() {
> List rowList = new ArrayList();
> for (Customer customer : customers.values()) {
> RowType row1 = new RowType();
> row1.setId(customer.getId());
> for (Product product : products.values()) {
> if (customer.getId().equals(product.getCustomerID())) {
> final String productId = Long.toString(product.getId());
>
> RowType row11 = new RowType();
> row11.setId(productId);
>
> row1.getRow().add(row11);
> }
>
> }
> rowList.add(row1);
> }
> RowsType rows = new RowsType();
> rows.getRow().addAll(rowList);
> return rows;
> }
>
>
> The JAXB generated class from the schema is shown below.
>
> public class RowsType {
> protected List<RowType> row;
>
> /**
> * Gets the value of the row property.
> *
> * <p>
> * This accessor method returns a reference to the live list,
> * not a snapshot. Therefore any modification you make to the
> * returned list will be present inside the JAXB object.
> * This is why there is not a <CODE>set</CODE> method for the row
> property.
> *
> * <p>
> * For example, to add a new item, do as follows:
> * <pre>
> * getRow().add(newItem);
> * </pre>
> *
> *
> * <p>
> * Objects of the following type(s) are allowed in the list
> * {_at_link RowType }
> *
> *
> */
> public List<RowType> getRow() {
> if (row == null) {
> row = new ArrayList<RowType>();
> }
> return this.row;
> }
>
> }
>
> When I try to start the REST service and invoke the "list"
> operation, I see the following message on the console.
>
> May 12, 2008 9:23:52 AM
> com.sun.ws.rest.impl.application.MessageBodyFactory
> getMessageBodyWriter
> SEVERE: A message body writer for Java type, class
> impl.products.jaxb.generated.RowsType, and MIME media type,
> application/xml, was not found
>
> How do I implement a MessageBodyWriter for the above JAXB generated
> type RowsType?
>
> Please clarify this behavior.
>
> Thanks!
> Arul
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>

---
Marc Hadley <marc.hadley at sun.com>
CTO Office, Sun Microsystems.