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








Wednesday, December 3, 2008

Converting Date from xml unmarshalled format to the standard representation.

Converting from "2008-12-03T12:46:35.896+02:00" to "Wed Dec 03 12:46:35 GMT+02:00 2008".


String dt = "yyyy-MM-dd'T'HH:mm:ss'.'SSSZZZZZ";
SimpleDateFormat sdf = new SimpleDateFormat(dt);
String outDateString = source.substring(0, 26) + source.substring(27, 29);
Date outDate = sdf.parse(outDateString);

Convert a String into an Element

Convert a String into an InputStream

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

StringBuffer stringBuffer1 = new StringBuffer("2008-12-03T13:24:00.408+02:00");
ByteArrayInputStream bis1 = new ByteArrayInputStream(stringBuffer1.toString()
.getBytes("UTF-8"));

Convert an InputStream into Document
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(bis1);

As a result of Element

org.w3c.dom.Element messageElement = document.getDocumentElement();