无法使用Apache Camel 3.1.0将XML转换为JSON

1cklez4t  于 2022-11-07  发布在  Apache
关注(0)|答案(2)|浏览(252)

我正在学习ApacheCamel,我需要学习这个框架,以便在我目前的工作中使用它。
书中使用的Apache Camel版本是2. 12. 4,但我无意中用3. 1. 0版本做了很多PoC。
现在我尝试将XML文件转换为JSON文件,但没有任何结果。
我的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.redhat</groupId>
    <artifactId>apachecamel-learning</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <dependencies>
        <!-- Apache CAMEL -->
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-core</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.30</version>
        </dependency>

        <!-- MARSHALL AND UNMARSHALL -->
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-jackson</artifactId>
            <version>3.1.0</version>
        </dependency>

        <!-- Camel JMS -->
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-jms</artifactId>
            <version>2.5.0</version>
        </dependency>

        <!-- ActiveMQ -->
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-core</artifactId>
            <version>5.3.2</version>
        </dependency>

        <!-- OSGi -->
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.core</artifactId>
            <version>4.3.1</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>
</project>

我的Java类

package com.redhat.pocs.camel.eip;

import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.model.dataformat.JacksonXMLDataFormat;

public class TestMarshallAndUnmarshall {

    public static void main(String[] args) throws Exception {
        CamelContext context = new DefaultCamelContext();
        context.addRoutes(new RouteBuilder() {
            public void configure() {
                from("file:/tmp/in")
                .marshal(new JacksonXMLDataFormat())
                .to("file:/tmp/out");
            }
        });
        context.start();
        Thread.sleep(10000); // Prevent to Camel to shutdown and don't run the route
        context.stop();
        context.close();
    }

}

我尝试使用XmlJsonDataFormat、JaxbDataFormat、JacksonXMLDataFormat,但总是出现相同的错误:

Exception in thread "main" org.apache.camel.FailedToCreateRouteException: Failed to create route route1 at: >>> Marshal[org.apache.camel.model.dataformat.JacksonXMLDataFormat@6e4784bc] <<< in route: Route(route1)[From[file:/tmp/in] -> [Marshal[org.apache.came... because of Data format 'jacksonxml' could not be created. Ensure that the data format is valid and the associated Camel component is present on the classpath

有人知道我做错了什么吗?

cgfeq70w

cgfeq70w1#

Camel 2号

从2.10.x版本开始,有了camel-xmljson组件。
我这样使用它(版本2.22.2)
Camel 路线:

final XmlJsonDataFormat xmlJsonFormat = new XmlJsonDataFormat();
    xmlJsonFormat.setRootName("result");

    CamelContext context = new DefaultCamelContext();
    context.addRoutes(new RouteBuilder() {
        public void configure() {
            from("file:/tmp?fileName=in.xml")
            .marshal(xmlJsonFormat)              
            .to("file:/tmp?fileName=out.json")
            .end();
        }
    });

    context.start();
    Thread.sleep(5000); // Prevent to Camel to shutdown and don't run the route
    context.stop();

Pom.xml:

...
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-core</artifactId>
      <version>${camel.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-xmljson</artifactId>
      <version>${camel.version}</version>
    </dependency>
    <dependency>
      <groupId>xom</groupId>
      <artifactId>xom</artifactId>
      <version>1.2.5</version>
    </dependency>
...

Camel 3号
camel-xmljson组件对于这个版本的camel是不推荐使用的。我找不到一个直接的方法来从xml转换到json。
不幸的是,我完成这项工作的唯一方法是使用一个中间的pojo。所以首先我从xml转换到Pojo,然后从Pojo转换到json。
Camel 路线:

JacksonDataFormat jacksonDataFormat = new JacksonDataFormat();
      jacksonDataFormat.setPrettyPrint(true);
      jacksonDataFormat.enableFeature(SerializationFeature.WRAP_ROOT_VALUE);

      CamelContext context = new DefaultCamelContext();
      context.addRoutes(new RouteBuilder() {
          public void configure() {
              from("file:/tmp?fileName=in.xml")
              .unmarshal().jacksonxml(Order.class)
              .marshal(jacksonDataFormat)
              .to("file:/tmp?fileName=out.json")
              .end();
          }
      });
      context.start();
      Thread.sleep(10000); // Prevent to Camel to shutdown and don't run the route
      context.stop();
      context.close();

订单和项目分类

public class Order {

  @JacksonXmlProperty(localName = "name")
  private String name;

  @JacksonXmlProperty(localName = "items")
  private List<Item> items;

  public Order() {

  }

  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }

  public List<Item> getItems() {
    return items;
  }

  public void setItems(List<Item> items) {
    this.items = items;
  }

}

public class Item {
  @JacksonXmlProperty(localName = "name")
  private String name;

  public Item() {

  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

}

Order.xml

<order>
    <name>123</name>
    <items>
        <item>
            <name>a</name>
        </item>
        <item>
            <name>b</name>
        </item>
    </items>
</order>

结果顺序. json

{
  "Order" : {
    "name" : "123",
    "items" : [ {
      "name" : "a"
    }, {
      "name" : "b"
    } ]
  }
}

希望能有所帮助。

6ie5vjzr

6ie5vjzr2#

xj

(mike下降)
我正在寻找相同的信息,几乎意外地发现了糟糕的名称“camel-xj”。https://camel.apache.org/components/3.18.x/xj-component.html
XJ组件允许您直接来回转换XML和JSON文档,而不需要中间的java对象。您甚至可以指定一个XSLT样式表来直接转换为目标JSON / XML(域)模型。

相关问题