2

I am new to Apache camel and need to perform a task where I need to marshal an object to XML file. I am using the below code but it is not working. Here, foo.pojo is package where JAXB annotated classes are present

JaxbDataFormat jaxbDataFormat =  new JaxbDataFormat("foo.pojo");
from("direct:start").marshal(jaxbDataFormat).to("file:C:/Users/Anand.Jain/Desktop/hello/abc.xml").end();
halfer
  • 19,824
  • 17
  • 99
  • 186
Anand
  • 20,708
  • 48
  • 131
  • 198

2 Answers2

5

Option 1: Configure the context path

JaxbDataFormat jaxbDataFormat =  new JaxbDataFormat("foo.pojo");

OptionFactory or jaxb.index file must be defined in the given package as explained here.

Option 2: Configure the class(es) to be bound

JAXBContext jaxbContext = JAXBContext.newInstance(MyAnnotatedClass.class);
JaxbDataFormat jaxbDataFormat = new JaxbDataFormat(jaxbContext);

I prefere Option 2.

Community
  • 1
  • 1
Peter Keller
  • 7,526
  • 2
  • 26
  • 29
2

Option 1 is recently impossible as JaxbDataFormat(String) constructor is not available as you can see in official javadoc

Documentation seems to be outdated about this point.

EDIT: BE CAREFUL there's TWO JaxbDataFormat

I have understood: there's TWO jaxbDataFormat in camel ecosystem

  • one is in camel-core package org.apache.camel.model.dataformat
  • another in camel-jaxb package org.apache.camel.converter.jaxb
MarcDeXeT
  • 264
  • 2
  • 14
  • And option 2 too :/ because of refactoring of JaxbDataFormat. package org.apache.camel.converter.jaxb.JaxbDataFormat seems to be moved to org.apache.camel.model.dataformat in camel-core 2.14..1 – MarcDeXeT Mar 13 '15 at 08:59