users@jersey.java.net

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

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

Hi Marc,

Thanks for the great help. I would like to fix the schema so that it
generates the @XmlRootElement in my JAXB type. What am I missing in this
schema to achieve this behavior?

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified"
elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="rows" type="rowsType"/>
  <xs:complexType name="rowsType">
    <xs:sequence>
      <xs:element type="rowType" name="row" maxOccurs="unbounded"
minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="cellType">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute type="xs:string" name="image" use="optional"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="userdataType">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute type="xs:string" name="name" use="optional"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="rowType">
    <xs:sequence>
      <xs:element type="userdataType" name="userdata"
maxOccurs="unbounded" minOccurs="0"/>
      <xs:element type="cellType" name="cell" maxOccurs="unbounded"
minOccurs="0"/>
      <xs:element type="rowType" name="row" maxOccurs="unbounded"
minOccurs="0"/>
    </xs:sequence>
    <xs:attribute type="xs:string" name="id" use="optional"/>
    <xs:attribute type="xs:string" name="open" use="optional"/>
  </xs:complexType>
</xs:schema>

Thanks!
Arul

Marc Hadley wrote:
> 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.
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>
>
> ________________________________
> Scanned by MessageLabs for Flux
> ________________________________