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/jaxb-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.
>
>
>