簡體   English   中英

XML以在Apache Camel中進行對象

[英]XML to object in Apache Camel

我正在嘗試獲取www.dnb.no/portalfront/datafiles/miscellaneous/csv/kursliste_ws.xml並使用xstrem將其轉換為Java對象。

我收到此錯誤:

線程“主”中的異常java.lang.NoClassDefFoundError:org / apache / camel / spi / DataFormatName造成原因: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>

我該如何轉換?

您在pom.xml中混合了駱駝組件的版本。 Camel-xstream是2.20,駱駝核心是2.15.1.redhat-621159。

2.16.x版本中引入了類路徑中缺少的類DataFormatName

所有駱駝依賴項更新到相同的版本,最好是2.20,它應該可以工作

老實說,我不了解XML到Java對象轉換的目的。 如果您使用Camel-Http組件讀取XML,則可以直接將其通過管道傳遞到ActiveMQ,這樣就可以正常工作。

在這種情況下,XStream的目的是什么?

我已經有了在玩具項目上實際運行的路線,並將XML注入了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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM