Apache Camel中不推荐使用的XmlJsonDataFormat的替代方法 [英] Alternative to deprecated XmlJsonDataFormat in Apache Camel
问题描述
在Apache Camel中,我试图使用XmlJsonDataFormat进行从XML到JSON的快速转换.但是,不推荐使用XMLJSON( http://camel.apache.org/xmljson.html )因此,除了创建处理器之外,我不确定进行转换的最佳方法是什么.
In Apache Camel, I was trying to use XmlJsonDataFormat to do a quick conversion from XML to JSON. However the XMLJSON (http://camel.apache.org/xmljson.html) has deprecated so I am not sure what is the best way to do that conversion aside from creating a processor.
有人知道XmlJsonDataFormat的替代方法吗?
Does anyone know of an alternative to XmlJsonDataFormat?
推荐答案
经过几次尝试,我成功地组合了Jaxb和Jackson,将XML解组到POJO,然后将POJO解构到JSON,从而得到了所需的东西.正如noMad所指出的,我本可以尝试使用Dozer( http://camel.apache.org/dozer.html ),但没有时间这样做.
After some tries, I have managed to get what I need by combining Jaxb and Jackson for unmarshal XML to POJO and then POJO to JSON. As noMad pointed out, I could have tried Dozer (http://camel.apache.org/dozer.html) as well but haven't got time to do so.
JacksonDataFormat jacksonDataFormat = new JacksonDataFormat();
jacksonDataFormat.setPrettyPrint(true);
jacksonDataFormat.enableFeature(SerializationFeature.WRAP_ROOT_VALUE);
from("file:src/main/resources/xml/in?noop=true").routeId("lixi-to-json-route").
unmarshal(new JaxbDataFormat(JAXBContext.newInstance(ApplicationBatch.class))).
marshal(jacksonDataFormat).
to("file:src/main/resources/xml/out?fileName=${file:onlyname.noext}.json");
Maven依赖项
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jaxb</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jackson</artifactId>
</dependency>
这篇关于Apache Camel中不推荐使用的XmlJsonDataFormat的替代方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!