且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Apache Camel 中已弃用的 XmlJsonDataFormat 的替代方法

更新时间:2023-02-19 14:03:32

经过一些尝试,我通过将 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>