问题
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();
Please help.
回答1:
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.
回答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
来源:https://stackoverflow.com/questions/26800182/jaxb-marshalling-in-apache-camel