Friday, December 5, 2008

SAXException2: missing an @XmlRootElement annotation

PROBLEM
Caused by: com.sun.istack.SAXException2: unable to marshal type "org.exoplatform.training.portlet.eventing.AddressBookEntry" as an element because it is missing an @XmlRootElement annotation
at com.sun.xml.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:244)
at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot(ClassBeanInfoImpl.java:303)
at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:490)
at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:328)
... 67 more
[com.sun.istack.SAXException2: unable to marshal type "org.exoplatform.training.portlet.eventing.AddressBookEntry" as an element because it is missing an @XmlRootElement annotation]
at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:331)
at com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:257)
at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:75)
at org.exoplatform.services.portletcontainer.plugins.pc.portletAPIImp.StateAwareResponseImp.validateWithJAXB(StateAwareResponseImp.java:244)
at org.exoplatform.services.portletcontainer.plugins.pc.portletAPIImp.StateAwareResponseImp.setEvent(StateAwareResponseImp.java:263)

SOLUTION

As the error message suggests, one way is to put @XmlRootElement on your class. Another way is to use JAXBElement like this:

marshaller.marshal( new JAXBElement(new QName("nsUri","local"), AddNumbersResponse.class, result), ... );


Sample at here MyEventPub

Thanks to kohsuke at forums.java.net








1 comment:

Don Cruz said...

Hi, im using netbeans 6.7 to create WebService Client, so i got a similar Exception:




[com.sun.istack.SAXException2: unable to marshal type "com.microsoft.schemas.exchange.services._2006.types.PathToUnindexedFieldType" as an element because it is missing an @XmlRootElement annotation]

when I modify PathToUnindexedFieldType.java with @XmlRootElementa and build the project, wsimport overwrite my changes.