2017-10-16 116 views
0

我试图获取www.dnb.no/portalfront/datafiles/miscellaneous/csv/kursliste_ws.xml并使用xstrem将其转换为Java对象。将XML映射到Apache Camel中的对象

我得到这个错误:

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> 

我如何转换呢?

+0

你是如何运行的呢? – awd

+0

右键单击IntelliJ中的代码并单击“运行CurrencyRoute.main”。 – Solo

+0

哦..然后我不知道。不知何故程序运行时依赖关系不在类路径中。 – awd

回答

1

老实说,我不明白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> 
1

你在你的pom.xml中混合了骆驼组件的版本。 Camel-xstream是2.20,骆驼核心是2.15.1.redhat-621159。

在2.16.x版本中引入了类路径中缺少的类DataFormatName

更新所有你的骆驼依赖于相同的版本,最好是2.20,它应该工作