【发布时间】:2020-03-26 19:53:43
【问题描述】:
我正在通过 Jean-Baptiste 的Mastering Apache Camel一书学习 Apache Camel。我需要学习这个框架,以便在我目前的工作中使用它。
书中使用的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
有人知道我做错了什么吗?
【问题讨论】:
-
你的“camel-jackson”依赖有一个 test 范围,而你的类似乎不是单元测试?
-
是的,对不起....我错过了那个细节。即使删除测试范围,错误也是一样的:原因:java.lang.IllegalArgumentException:无法创建数据格式“jacksonxml”。确保数据格式有效并且类路径中存在关联的 Camel 组件
-
Underscore-java库可以将xml转json。 U.xmlToJson(xml) 方法会有所帮助。
标签: java json xml apache-camel