users@jersey.java.net

RE: [Jersey] usage of third party pojos in restful services

From: Suchitha Koneru (sukoneru) <"Suchitha>
Date: Tue, 15 Dec 2009 19:40:01 -0800

Thanks Paul for the work around, I have implemented it. However the
issue was something else as explained below.

  After further debugging it became apparent that the issue is due to
JAXB version inconsistency. The code was compiled against JAXB 2.2 and
in the Run time (Application server Jboss 5.1.0 GA and Java 1.5) the
available version of JAXB was 2.1.


There is no staright forward approach to determine that this error
occurred due to JAXB version inconsistency as it is not evident from the
following error message

" A message body writer for Java type, class java.util.ArrayList, and
 MIME media type, application/xml, was not found"


I changed the JAXB version to 2.1 during compilation ,re deployed the
ear and everything works fine and I do not see any issues.

Is there a way we can enhance Jersey error reporting such that , they
can display the exact cause and corresponding error messages.


Thanks,
Suchitha

-----Original Message-----
From: Paul.Sandoz_at_Sun.COM [mailto:Paul.Sandoz_at_Sun.COM]
Sent: Wednesday, December 09, 2009 2:19 AM
To: users_at_jersey.dev.java.net
Subject: Re: [Jersey] usage of third party pojos in restful services

Hi Suchitha,

Jersey/JAXB integration does not support the serialization of a XML
type bean annotated with @XmlType, or a list of those, because Jersey
does not know how to derive the element name to use when marshaling
the XML type bean. When using JAX-WS that information is specified in
the WSDL.

Jersey can serialize List<T> where T is a XML root element bean
annotated with @XmlRootElement. In such a case Jersey derives the root
element of the XML document from the class name of T.

Jersey can serialize an instance of JAXBElement<T>, but it cannot
serialize List<JAXBElement<T>>, although it should be easy to modify
to support that.

See the following sample for what is currently supported:

 
http://download.java.net/maven/2/com/sun/jersey/samples/jaxb/1.1.4.1/jax
b-1.1.4.1-project.zip

So in your case you have two options:

1) Create a list wrapper root element bean that holds the list of
AlertMemoryData and define, using JAXB annotations, what
      the element name should be for the items in the list; or

2) Create a wrapper root element bean that wraps AlertMemoryData and
then create a list of those wrappers.

Hth,
Paul.

On Dec 8, 2009, at 7:42 AM, Suchitha Koneru (sukoneru) wrote:

> Hello Jersey Users ,
> I have landed into the following exception when trying
> to use java beans present in a jar file.
> "A message body writer for Java type, class java.util.ArrayList, and
> MIME media type, application/xml, was not found"
>
> The Restful API is
>
> @GET @Path ("alerts")
> @Produces("application/xml")
>
> public List<AlertMemoryData> getAlertData(){
>
> ArrayList<AlertMemoryData> list = new ArrayList
> <AlertMemoryData>();
> Random r = new Random();
>
> for(int i=0;i<5;i++){
> AlertMemoryData alertMemData = new
> AlertMemoryData();
> alertMemData.setAdditionalDataId(r.nextInt());
> alertMemData.setAmdMemoryId(r.nextLong());
> alertMemData.setAmdTimestamp(r.nextLong());
> alertMemData.setAmdValue(r.nextInt());
> list.add(alertMemData);
> }
> return list;
>
> }
>
> Here AlertMemoryData is a java pojo defined elsewhere and is
> included in the form of a jar in web-inf/lib. I suspected that
> something must be going
> wrong with marshalling to xml using JAXB
>
> I also tried to explicitly serialize the pojo as follows
>
> AlertMemoryData memData = new AlertMemoryData();
> JAXBContext jc = JAXBContext.newInstance(AlertMemoryData.class);
> Marshaller m = jc.createMarshaller();
> ByteArrayOutputStream bos = new ByteArrayOutputStream();
> m.marshall(memData, bos);
> return new String(bos.toByteArray());
>
> This led to an exception
> com.sun.istack.SAXException2: unable to marshal type
> "com.cisco.ca.ast.service.pollingsvcs.datamodel.AlertMemoryData" as
> an element because it is missing an @XmlRootElement annotation]
>
>
> The java src code for AlertMemoryData(AlertMemoryData.java) has
> @XMlRootElement annotation. I am not sure why the class file does
> not have it.
>
>
> Finally I tried to serialize without the need for the presence of
> the annotation @XMLRootElement , as shown below
>
> JAXBContext jc = JAXBContext.newInstance(AlertMemoryData.class);
> Marshaller m = jc.createMarshaller();
> ByteArrayOutputStream bos = new ByteArrayOutputStream();
> m.marshal(new JAXBElement<AlertMemoryData>(new QName("uri","local"),
> AlertMemoryData.class, memData),bos);
>
> This approach worked
>
> Is this a JAXB or a Jersey issue ? Could you please let me know,
> if we can configure Jersey to use the second approach of marshalling
> as it does not need @XMlRootElement annotation.
>
> I also tried to invoke the same API (getAlertData() shown above) via
> a Jaxws webservice and it worked out of box without any issues.
>
> Thank you,
> Suchitha.
>
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
For additional commands, e-mail: users-help_at_jersey.dev.java.net