Friday, February 7, 2014

javax.xml.bind.MarshalException

javax.xml.bind.MarshalException
 - with linked exception:
[com.sun.istack.SAXException2: unable to marshal type "com.javaxp.Employee" 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:96)
at com.hewitt.ws.Test.marshalIt(Test.java:74)
at com.hewitt.ws.Test.displayEmpl(Test.java:58)
at com.hewitt.ws.Test.main(Test.java:28)
Caused by: com.sun.istack.SAXException2: unable to marshal type "com.hewitt.ws.Employee" 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)
... 5 more

The process of convesrting a Java object to XML is known as Marshalling.

If you get above error, check for annotation @XmlRootElement in the class for which you are trying to do Marshalling. To know more, please see the example - JAXB marshalling and unmarshalling Hello World Example

Example - 

@XmlRootElement
public class Employee {
...
}