Apache camel 我如何才能成功地使用“Jackson”;ObjectMapper();在a内;过程();方法

Apache camel 我如何才能成功地使用“Jackson”;ObjectMapper();在a内;过程();方法,apache-camel,jbossfuse,blueprint-osgi,camel-blueprint,Apache Camel,Jbossfuse,Blueprint Osgi,Camel Blueprint,问题:在“process()”方法中使用Jackson的“ObjectMapper()”会导致异常… 异常显示在Jboss Fuse控制台中…如下所示 问题: 如何在“process()”方法中成功地使用Jackson“ObjectMapper()”,即将映射转换为json 为了提供上下文,下面是有关此简单应用程序/捆绑包的信息 aaa.bbb.ccc.CamelRestRoutes.java package aaa.bbb.ccc; import org.apache.camel.Exc

问题:在“process()”方法中使用Jackson的“ObjectMapper()”会导致异常…

异常显示在Jboss Fuse控制台中…如下所示

问题: 如何在“process()”方法中成功地使用Jackson“ObjectMapper()”,即将映射转换为json



为了提供上下文,下面是有关此简单应用程序/捆绑包的信息

aaa.bbb.ccc.CamelRestRoutes.java

package aaa.bbb.ccc;

import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.cdi.ContextName;

@ContextName("rest-dsl")
public class CamelRestRoutes extends RouteBuilder {

    public CamelRestRoutes() {
    }

    private final org.apache.camel.Processor proc1 = new Processor1();

    int notificationTime = 60;
    int overlapTime = 5;    
    String codeListString = "AA,BB";

    @Override
    public void configure() throws Exception {

    String fromURL = "http://localhost:7001/jaxrsRestService/service/getAll"; 

    from("timer://foo?fixedRate=true&period=" + 5000) //5 seconds... do we need to make this a system property (like notification service)???....
        .setBody(constant(codeListString))            
        .to("direct:thingB");

    from("direct:thingB")
        .split().tokenize(",")
        .to("direct:thingC");       

    from("direct:thingC")
        .setHeader(Exchange.HTTP_METHOD, constant(org.apache.camel.component.http4.HttpMethods.GET))
        .to("netty4-http:http://localhost:7001/jaxrsRestService/service/getAll/")
        .to("direct:thingD");

    from("direct:thingD")
        .split()
        .jsonpath("$.container[*]")
        .streaming()
        .parallelProcessing()
        .process(proc1)
        .to("wmq:queue:mylocalqueue?jmsMessageType=Text&exchangePattern=InOnly");
    }
}
package aaa.bbb.ccc;

import java.io.IOException;
import java.util.Map;

public class Processor1 implements org.apache.camel.Processor {

    @Override
    public void process(org.apache.camel.Exchange exchange) throws IOException {

    //***this causes the exception***
    com.fasterxml.jackson.databind.ObjectMapper.ObjectMapper om = new com.fasterxml.jackson.databind.ObjectMapper.ObjectMapper();  <== causes

    Map<String, String> map = (Map<String, String>) exchange.getIn().getBody();

    exchange.getIn().setBody(map.toString());
    }
};
aaa.bbb.ccc.Processor1.java

package aaa.bbb.ccc;

import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.cdi.ContextName;

@ContextName("rest-dsl")
public class CamelRestRoutes extends RouteBuilder {

    public CamelRestRoutes() {
    }

    private final org.apache.camel.Processor proc1 = new Processor1();

    int notificationTime = 60;
    int overlapTime = 5;    
    String codeListString = "AA,BB";

    @Override
    public void configure() throws Exception {

    String fromURL = "http://localhost:7001/jaxrsRestService/service/getAll"; 

    from("timer://foo?fixedRate=true&period=" + 5000) //5 seconds... do we need to make this a system property (like notification service)???....
        .setBody(constant(codeListString))            
        .to("direct:thingB");

    from("direct:thingB")
        .split().tokenize(",")
        .to("direct:thingC");       

    from("direct:thingC")
        .setHeader(Exchange.HTTP_METHOD, constant(org.apache.camel.component.http4.HttpMethods.GET))
        .to("netty4-http:http://localhost:7001/jaxrsRestService/service/getAll/")
        .to("direct:thingD");

    from("direct:thingD")
        .split()
        .jsonpath("$.container[*]")
        .streaming()
        .parallelProcessing()
        .process(proc1)
        .to("wmq:queue:mylocalqueue?jmsMessageType=Text&exchangePattern=InOnly");
    }
}
package aaa.bbb.ccc;

import java.io.IOException;
import java.util.Map;

public class Processor1 implements org.apache.camel.Processor {

    @Override
    public void process(org.apache.camel.Exchange exchange) throws IOException {

    //***this causes the exception***
    com.fasterxml.jackson.databind.ObjectMapper.ObjectMapper om = new com.fasterxml.jackson.databind.ObjectMapper.ObjectMapper();  <== causes

    Map<String, String> map = (Map<String, String>) exchange.getIn().getBody();

    exchange.getIn().setBody(map.toString());
    }
};
部署包含IBM MQ osgi JAR的目录


谢谢你的帮助

您遇到的部署错误是因为类路径上没有正确版本的com.fasterxml.jackson.databindjar(如下面的所定义)。您是否正在使用功能文件进行部署?如果是,请将其包括在您的问题中

org.osgi.framework.BundleException:捆绑包中未解析的约束 aaa.bbb.ccc.camelRest[585]:无法解析585.0:缺少 要求[585.0]osgi.wiring.package; (&(osgi.willing.package=com.fasterxml.jackson.databind)(版本>=2.7.0)(!(版本>=3.0.0)


使用Processor反序列化为JSON的方法是正确的,但是我建议在Process()方法之外创建ObjectMapper(),因为您不需要每次都创建一个新的ObjectMapper,而且创建一个ObjectMapper的成本很高

谢谢java-301!有道理我会在这个周末试一试,然后给你一个状态!不使用“功能文件”进行部署。已安装“camel jackson”(即“camel-2.17.0.redhat-630187”)。注意:我已经为IBM MQ 8.x添加了OSGI捆绑包(到部署文件夹)。这些捆绑包可能会导致某种冲突吗?多亏了你的建议,最终解决了这个问题-thx。必须查找redhat版本的camel jackson在使用什么…在此页()上找到它。然后将pom.xml依赖项更改为相同的:“com.fasterxml.jackson.core jackson databind 2.6.3”感谢后续saim,很高兴能提供帮助。有时,这些OSGI错误可能是模糊的。
<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0"
       xmlns:jaxrs="http://cxf.apache.org/blueprint/jaxrs"
       xmlns:camel="http://camel.apache.org/schema/blueprint"
       xsi:schemaLocation="
         http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
         http://cxf.apache.org/blueprint/jaxrs http://cxf.apache.org/schemas/blueprint/jaxrs.xsd
         http://cxf.apache.org/blueprint/core http://cxf.apache.org/schemas/blueprint/core.xsd
         ">
    <camel:camelContext id="aaa.bbb.ccc.routing.poc" xmlns="http://camel.apache.org/schema/blueprint">
    <packageScan>
        <package>aaa.bbb.ccc</package>
    </packageScan>
    </camel:camelContext>

    <bean id="wmqcf" class="com.ibm.mq.jms.MQConnectionFactory">
    <property name="hostName" value="localhost"/>        
    <property name="port" value="1414"/>
    <property name="queueManager" value="QM1"/>     
    <property name="channel" value="DEV.ADMIN.SVRCONN"/>                     
    <property name="transportType" value="1"/>
    </bean>

    <bean id="wmqcfw"  class="org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter">
    <property name="targetConnectionFactory" ref="wmqcf" />
    <property name="username" value="admin" />
    <property name="password" value="passw0rd" />
    </bean>  

    <bean id="wmqcfg" class="org.apache.camel.component.jms.JmsConfiguration">
    <property name="connectionFactory" ref="wmqcfw"/>
    <property name="concurrentConsumers" value="10"/>
    </bean>

    <bean id="wmq" class="org.apache.camel.component.jms.JmsComponent">
    <property name="configuration" ref="wmqcfg"/>     
    </bean>    
</blueprint>
<?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>aaa.bbb.ccc</groupId>
    <artifactId>camelRest</artifactId>
    <version>1</version>
    <packaging>bundle</packaging>
    <name>camelRest</name>
    <description>camelRest</description>

    <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <skipTests>true</skipTests>
    <mq.version>8.0.0.7</mq.version>
    </properties>

    <dependencies>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.25</version>
        <scope>provided</scope>               
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.25</version>
        <scope>provided</scope>               
    </dependency>
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-core</artifactId>
        <version>2.17.0</version>
        <scope>provided</scope>
    </dependency>     
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-cdi</artifactId>
        <version>2.17.0</version> 
        <scope>provided</scope>                     
    </dependency>    
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-netty4-http</artifactId>
        <version>2.17.0</version> 
        <scope>provided</scope>                     
    </dependency>
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-jackson</artifactId>
        <version>2.17.0</version>
        <scope>provided</scope>               
    </dependency>      
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-http4</artifactId>
        <version>2.17.0</version>
        <scope>provided</scope>
    </dependency>       
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-cxf</artifactId>
        <version>2.17.0</version>
        <scope>provided</scope>              
    </dependency>
    <dependency>
        <groupId>com.ibm.mq</groupId>
        <artifactId>allclient</artifactId>
        <version>${mq.version}</version>
        <scope>provided</scope>               
    </dependency>
    <dependency>
        <groupId>com.ibm.mq</groupId>
        <artifactId>jms</artifactId>
        <version>${mq.version}</version>
        <scope>provided</scope>
    </dependency>  
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-jsonpath</artifactId>
        <version>2.17.0</version>
        <scope>provided</scope>
    </dependency>                        

    </dependencies>

    <build>
    <finalName>${project.artifactId}-${project.version}</finalName>

    <resources>
        <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
        </resource>
    </resources>

    <plugins>

        <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
            <showDeprecation>true</showDeprecation>
        </configuration>
        </plugin> 

        <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>jaxb2-maven-plugin</artifactId>
        <version>2.3</version>
        <executions>
            <execution>
            <id>xjc</id>
            <goals>
                <goal>xjc</goal>
            </goals>
            </execution>
        </executions>        
        <configuration>
            <sources>
            <source>src/main/resources/xsd/jaxrsRestService.xsd</source>
            </sources>
            <packageName>aaa.bbb.ccc.generated</packageName>                    
            <verbose default-value="false">${xjc.verbose}</verbose>
        </configuration>                
        </plugin>              

        <plugin>
        <groupId>org.apache.felix</groupId>
        <artifactId>maven-bundle-plugin</artifactId>
        <version>3.3.0</version>
        <extensions>true</extensions>
        <configuration>
            <instructions>
            <Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
            <Export-Package>aaa.bbb.ccc*</Export-Package> 
            <Export-Package>aaa.bbb.ccc.generated*</Export-Package>
            <Import-Package>*</Import-Package>                                                                      
            </instructions>
        </configuration>
        </plugin>             
    </plugins>
    </build>
</project>
JBossFuse:karaf@root> features:list | grep "camel-" | grep "\[installed"
[installed  ] [2.7.0                ] xml-specs-api                                 camel-2.17.0.redhat-630187
[installed  ] [2.17.0.redhat-630187 ] camel                                         camel-2.17.0.redhat-630187
[installed  ] [2.17.0.redhat-630187 ] camel-core                                    camel-2.17.0.redhat-630187
[installed  ] [2.17.0.redhat-630187 ] camel-blueprint                               camel-2.17.0.redhat-630187
[installed  ] [2.17.0.redhat-630187 ] camel-spring                                  camel-2.17.0.redhat-630187
[installed  ] [2.17.0.redhat-630187 ] camel-bindy                                   camel-2.17.0.redhat-630187
[installed  ] [2.17.0.redhat-630187 ] camel-cdi                                     camel-2.17.0.redhat-630187
[installed  ] [2.17.0.redhat-630187 ] camel-csv                                     camel-2.17.0.redhat-630187
[installed  ] [2.17.0.redhat-630187 ] camel-cxf                                     camel-2.17.0.redhat-630187
[installed  ] [2.17.0.redhat-630187 ] camel-exec                                    camel-2.17.0.redhat-630187
[installed  ] [2.17.0.redhat-630187 ] camel-ftp                                     camel-2.17.0.redhat-630187
[installed  ] [2.17.0.redhat-630187 ] camel-http4                                   camel-2.17.0.redhat-630187
[installed  ] [2.17.0.redhat-630187 ] camel-jackson                                 camel-2.17.0.redhat-630187
[installed  ] [2.17.0.redhat-630187 ] camel-jacksonxml                              camel-2.17.0.redhat-630187
[installed  ] [2.17.0.redhat-630187 ] camel-jasypt                                  camel-2.17.0.redhat-630187
[installed  ] [2.17.0.redhat-630187 ] camel-jaxb                                    camel-2.17.0.redhat-630187
[installed  ] [2.17.0.redhat-630187 ] camel-jdbc                                    camel-2.17.0.redhat-630187
[installed  ] [2.17.0.redhat-630187 ] camel-jms                                     camel-2.17.0.redhat-630187
[installed  ] [2.17.0.redhat-630187 ] camel-jmx                                     camel-2.17.0.redhat-630187
[installed  ] [2.17.0.redhat-630187 ] camel-jsonpath                                camel-2.17.0.redhat-630187
[installed  ] [2.17.0.redhat-630187 ] camel-mail                                    camel-2.17.0.redhat-630187
[installed  ] [2.17.0.redhat-630187 ] camel-netty4                                  camel-2.17.0.redhat-630187
[installed  ] [2.17.0.redhat-630187 ] camel-netty4-http                             camel-2.17.0.redhat-630187
[installed  ] [2.17.0.redhat-630187 ] camel-ognl                                    camel-2.17.0.redhat-630187
[installed  ] [2.17.0.redhat-630187 ] camel-paxlogging                              camel-2.17.0.redhat-630187
[installed  ] [2.17.0.redhat-630187 ] camel-restlet                                 camel-2.17.0.redhat-630187
[installed  ] [2.17.0.redhat-630187 ] camel-rmi                                     camel-2.17.0.redhat-630187
[installed  ] [2.17.0.redhat-630187 ] camel-routebox                                camel-2.17.0.redhat-630187
[installed  ] [2.17.0.redhat-630187 ] camel-saxon                                   camel-2.17.0.redhat-630187
[installed  ] [2.17.0.redhat-630187 ] camel-script                                  camel-2.17.0.redhat-630187
[installed  ] [2.17.0.redhat-630187 ] camel-snmp                                    camel-2.17.0.redhat-630187
[installed  ] [2.17.0.redhat-630187 ] camel-spring-javaconfig                       camel-2.17.0.redhat-630187
[installed  ] [2.17.0.redhat-630187 ] camel-xstream                                 camel-2.17.0.redhat-630187
[installed  ] [1.2.0.redhat-630187  ] camel-amq                                     fabric-1.2.0.redhat-630187
JBossFuse:karaf@root>
Directory of C:\tools\jboss-fuse-6.3.0.redhat-187\deploy

09/13/2017  10:19 AM    <DIR>          .
09/13/2017  10:19 AM    <DIR>          ..
09/12/2017  05:10 PM            14,495 camelRest-1.jar
06/29/2017  01:00 AM           159,649 com.ibm.mq.osgi.allclientprereqs_8.0.0.7.jar
06/29/2017  01:00 AM         8,011,749 com.ibm.mq.osgi.allclient_8.0.0.7.jar
06/29/2017  01:00 AM         4,088,715 com.ibm.mq.osgi.java_8.0.0.7.jar
06/29/2017  01:00 AM           171,064 com.ibm.msg.client.osgi.commonservices.j2se_8.0.0.7.jar
06/29/2017  01:00 AM            48,715 com.ibm.msg.client.osgi.jms.prereq_8.0.0.7.jar.DISABLE
06/29/2017  01:00 AM           639,807 com.ibm.msg.client.osgi.jms_8.0.0.7.jar
06/29/2017  01:00 AM           216,218 com.ibm.msg.client.osgi.nls_8.0.0.7.jar
06/29/2017  01:00 AM           279,861 com.ibm.msg.client.osgi.wmq.nls_8.0.0.7.jar
06/29/2017  01:00 AM            92,406 com.ibm.msg.client.osgi.wmq.prereq_8.0.0.7.jar
06/29/2017  01:00 AM         7,963,226 com.ibm.msg.client.osgi.wmq_8.0.0.7.jar
09/15/2016  04:19 AM               873 README
          12 File(s)     21,686,778 bytes
           2 Dir(s)  142,681,493,504 bytes free
jdk1.8.0_131
jboss-fuse-6.3.0.redhat-187
WebLogic 12.2.1  (running the rest service)