[英]XML to Json with Camel
我的程序應該將XML文件格式轉換為JSON文件格式。 我試圖使用marshal命令,但我得到錯誤:
線程“main”中的異常org.apache.camel.FailedToCreateRouteException:無法在以下位置創建路由route1:>>> Marshal [org.apache.camel.model.dataformat.XmlJsonDataFormat@815b41f] <<< in route:Route(route1) [[來自[file:resource / inbox]] - > [Marshal [org.ap ...因為數據格式'xmljson'無法創建。 確保數據格式有效,並且類路徑中存在關聯的Camel組件
引起:java.lang.IllegalArgumentException:無法創建數據格式'xmljson'。 確保數據格式有效,並且類路徑中存在關聯的Camel組件
Java代碼
package route;
import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.model.dataformat.XmlJsonDataFormat;
import org.apache.camel.spi.DataFormat;
import org.apache.log4j.BasicConfigurator;
public class CopyToJsonRoute {
public static void main(String args[]) throws Exception {
// Log 4j
BasicConfigurator.configure();
// create CamelContext
CamelContext context = new DefaultCamelContext();
// add our route to the CamelContext
context.addRoutes(new RouteBuilder() {
public void configure() {
// http://www.yr.no/place/Norway/Oslo/Oslo/Oslo/forecast.xml
XmlJsonDataFormat xmlJsonFormat = new XmlJsonDataFormat();
xmlJsonFormat.setForceTopLevelObject(true);
from("file:resource/inbox")
.marshal(xmlJsonFormat)
.to("file:src/main/resources/data/output?autoCreate=true");
}
});
// start the route and let it do its work
context.start();
Thread.sleep(10000);
// stop the CamelContext
context.stop();
}
}
Pom文件
<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yourdomain.subdomain</groupId>
<artifactId>copy-to-json</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.camel/camel-core -->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<version>2.20.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.camel/camel-jackson -->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jackson</artifactId>
<version>2.20.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.8.0-alpha2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.camel/camel-jms -->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jms</artifactId>
<version>2.20.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/xom/xom -->
<dependency>
<groupId>xom</groupId>
<artifactId>xom</artifactId>
<version>1.2.5</version>
</dependency>
</dependencies>
</project>
您需要在Maven pom文件中添加camel-xmljson
作為依賴項
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-xmljson</artifactId>
<version>2.20.0</version>
</dependency>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.