Apache Camel中的JAXB编组

时间:2014-11-07 11:25:37

标签: jaxb apache-camel marshalling

我是Apache camel的新手,需要执行一项任务,我需要将对象编组到xml文件中。 我使用下面的代码,但它无法正常工作。这里,foo.pojo是包含JAXB注释类的包

JaxbDataFormat jaxbDataFormat =  new JaxbDataFormat("foo.pojo");
from("direct:start").marshal(jaxbDataFormat).to("file:C:/Users/Anand.Jain/Desktop/hello/abc.xml").end();

请帮忙。

2 个答案:

答案 0 :(得分:2)

选项1:配置上下文路径

JaxbDataFormat jaxbDataFormat =  new JaxbDataFormat("foo.pojo");
必须按照here所述,在给定的包中定义

OptionFactoryjaxb.index文件。

选项2:配置要绑定的类

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

我更喜欢选项2。

答案 1 :(得分:2)

最近不可能选项1,因为 JaxbDataFormat(String)构造函数不可用as you can see in official javadoc

文档似乎已经过时了。

编辑:小心,有两个JaxbDataFormat

我明白了:骆驼生态系统中有两个jaxbDataFormat

  • 一个在 camel-core org.apache.camel.model.dataformat
  • 另一个 camel-jaxb org.apache.camel.converter.jaxb