java - Apache Camel 中的 XML 对象

标签 java maven apache-camel

我正在尝试获取 www.dnb.no/portalfront/datafiles/miscellaneous/csv/kursliste_ws.xml并使用 xstrem 将其转换为 Java 对象。

我收到此错误:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/camel/spi/DataFormatName Caused by: java.lang.ClassNotFoundException: org.apache.camel.spi.DataFormatName

我的代码:

package route;

import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import mapper.CurrencyMapper;
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.log4j.BasicConfigurator;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.*;
import java.net.URL;

public class CurrencyRoute {

public static void main(String args[])  throws Exception {
    // Log 4j
    BasicConfigurator.configure();

    // Create camel context
    CamelContext context = new DefaultCamelContext();

    // New route
    context.addRoutes(new RouteBuilder() {
        public void configure() {

            from("quartz://myTimer?trigger.repeatCount=0")
                    .log("### Quartz trigger ###")
                    .to("direct:readFile");

            from("direct:readFile")
                    .log("### HTTP to XML ###")
                    .to("https4://www.dnb.no/portalfront/datafiles/miscellaneous/csv/kursliste_ws.xml")
                    .marshal()
                    .xstream()
                    .to("uri:activemq:queue:currency");
        }
    });

    // 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.test</groupId>
<artifactId>currencyProcessor</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
    <camel-version>2.15.1.redhat-621159</camel-version>
    <jaxb2-maven-plugin-version>2.2</jaxb2-maven-plugin-version>
    <maven-compiler-plugin-version>3.5.1</maven-compiler-plugin-version>

</properties>

<dependencies>
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-core</artifactId>
        <version>2.15.1.redhat-621159</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.8.0-alpha2</version>
    </dependency>

    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-ftp</artifactId>
        <version>2.15.1</version>
    </dependency>

    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-http</artifactId>
        <version>2.19.3</version>
    </dependency>

    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>spi-annotations</artifactId>
        <version>2.20.0</version>
    </dependency>

    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-http4</artifactId>
        <version>2.15.1.redhat-621159</version>
        <!-- use the same version as your Camel core version -->
    </dependency>

    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-quartz</artifactId>
        <version>2.15.1.redhat-621159</version>
        <!-- use the same version as your Camel core version -->
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-dataformat-xml</artifactId>
        <version>2.9.0</version>
    </dependency>

    <dependency>
        <groupId>org.codehaus.woodstox</groupId>
        <artifactId>woodstox-core-asl</artifactId>
        <version>4.4.1</version>
    </dependency>
    <dependency>
        <groupId>com.google.collections</groupId>
        <artifactId>google-collections</artifactId>
        <version>1.0</version>
    </dependency>

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

    <!-- https://mvnrepository.com/artifact/xom/xom -->
    <dependency>
        <groupId>xom</groupId>
        <artifactId>xom</artifactId>
        <version>1.2.5</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.camel/camel-xstream -->
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-xstream</artifactId>
        <version>2.20.0</version>
    </dependency>


    <!-- https://mvnrepository.com/artifact/xstream/xstream -->
    <dependency>
        <groupId>xstream</groupId>
        <artifactId>xstream</artifactId>
        <version>1.2.2</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.activemq/activemq-camel -->
    <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-camel</artifactId>
        <version>5.5.0</version>
    </dependency>


</dependencies>

如何转换它?

最佳答案

老实说,我不明白 XML 到 Java 对象转换的目的。如果您使用 Camel-Http 组件读取 XML,您可以将其直接传送到 ActiveMQ,它工作得很好。

XStream 在这种情况下的目的是什么?

我已经获得了实际在玩具项目上运行的路由,并将 XML 注入(inject)到了我的 ActiveMQ 队列中。

这是工作路线:

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jms.JmsComponent;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.log4j.BasicConfigurator;

import javax.jms.ConnectionFactory;

public class CurrencyRoute {

    public static void main(String args[]) throws Exception {
        // Log 4j
        BasicConfigurator.configure();

        // Create camel context
        CamelContext context = new DefaultCamelContext();

        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
        context.addComponent("test-jms", JmsComponent.jmsComponentAutoAcknowledge(connectionFactory));
        // New route
        context.addRoutes(new RouteBuilder() {
            public void configure() {

                from("quartz://myTimer?trigger.repeatCount=0")
                        .log("### Quartz trigger ###")
                        .to("direct:readFile");

                from("direct:readFile")
                        .log("### HTTP to XML ###")
                        .to("https://www.dnb.no/portalfront/datafiles/miscellaneous/csv/kursliste_ws.xml")
                        .to("test-jms:queue:currency");
            }
        });

        // start the route and let it do its work
        context.start();
        Thread.sleep(10000);

        // stop the CamelContext
        context.stop();


    }
}

这是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 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.onepointltd</groupId>
    <artifactId>camel.activemq</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>

    <name>Dummy test project</name>
    <url>http://www.onepointltd.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <log4j.version>2.7</log4j.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.apache.camel</groupId>
                <artifactId>camel-parent</artifactId>
                <version>2.17.3</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-jms</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-spring</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-quartz</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-jetty</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-http</artifactId>
        </dependency>

        <!-- the ActiveMQ client with connection pooling -->
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-client</artifactId>
            <version>5.14.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-camel</artifactId>
            <version>5.14.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-pool</artifactId>
            <version>5.14.0</version>
        </dependency>

        <!-- the ActiveMQ broker is optional and can be removed if connecting to a remote broker only -->
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-broker</artifactId>
            <version>5.14.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-spring</artifactId>
            <version>5.14.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-kahadb-store</artifactId>
            <version>5.14.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.xbean</groupId>
            <artifactId>xbean-spring</artifactId>
            <version>4.5</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.3.RELEASE</version>
        </dependency>

        <!-- logging -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>${log4j.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>${log4j.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>2.7</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-1.2-api -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-1.2-api</artifactId>
            <version>2.7</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-jsonpath</artifactId>
        </dependency>

        <!-- testing -->
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-test-spring</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.mockito/mockito-all -->
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.9.5</version>
        </dependency>

    </dependencies>
</project>

关于java - Apache Camel 中的 XML 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46766311/

相关文章:

java - String#intern 关于实习生结果的困惑

java - 检测JLabel中的哪个图像

eclipse - 应用程序在 eclipse 中运行良好,但在手动部署时无法启动

java - 从文本文件输入创建对象

java - 在 RESTEasy 中使用 POST 请求

java - Maven 冲突依赖 kafka-stream-test-utils 和 kafka-streams

java - Jersey Restful maven 项目出错

java - 如何在 Camel 中跨步骤使用相同的事务?

logging - 如何在 Camel 的 onException 子句中设置记录器(日志类别/日志名称)?

java - Apache Camel 如何获取输入的 xml 负载大小?