Friday, February 26, 2016

JAXB UnmarshalException when converting Payload in Camel

Camel provides camel-jaxb jar  for converting XML to POJO data and vice versa using JAXB data format.
The samples provided in the documentation states that the contextPath should be set to the Java Package of the POJO class for the marshaling and unmarshaling purposes.
But when trying to set the contextPath to a required class package  it throws the exception “javax.xml.bind.UnmarshalException: unexpected element Expected elements are (none)
This is raised because meta-data information is missing and the registryFormat element is not found in the context. This can be fixed by creating the Context with the Object Factory class and using this reference to unmarshal the data. 
JAXBContext jaxbContext = JAXBContext
                           .newInstance(new Class[] { com.test.ObjectFactory.class });
              DataFormat registryFormat = new JaxbDataFormat(jaxbContext);
              from("file:in?noop=true").convertBodyTo(org.w3c.dom.Document.class)
                           .unmarshal(registryFormat)
                           .log("The message after conversion" + "${body}");