users@jersey.java.net

returning JAXB type from a REST method

From: Arul Dhesiaseelan <arul_at_fluxcorp.com>
Date: Mon, 12 May 2008 09:26:26 -0600

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