2017-08-30 163 views
0

問題:使用傑克遜的「ObjectMapper()的」內「過程()」方法會導致異常...我怎樣才能成功地使用了傑克遜「ObjectMapper()」內的「過程()」方法

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

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

的異常在JBoss保險絲控制檯顯示...看起來像這樣...

... 

2017-09-12 16:45:56,262 | INFO | edhat-187/deploy | fileinstall      | 9 - org.apache.felix.fileinstall - 3.5.0 | Installing bundle aaa.bbb.ccc.camelRest/1.0.0 
2017-09-12 16:45:56,323 | WARN | edhat-187/deploy | fileinstall      | 9 - org.apache.felix.fileinstall - 3.5.0 | Error while starting bundle: file:/C:/tools/jboss-fuse-6.3.0.redhat-187/deploy/camelRest-1.jar 
org.osgi.framework.BundleException: Unresolved constraint in bundle aaa.bbb.ccc.camelRest [759]: Unable to resolve 759.0: missing requirement [759.0] osgi.wiring.package; (&(osgi.wiring.package=com.fasterxml.jackson.databind)(version>=2.7.0)(!(version>=3.0.0))) 
    at org.apache.felix.framework.Felix.resolveBundleRevision(Felix.java:4002)[org.apache.felix.framework-4.4.1.jar:] 
    at org.apache.felix.framework.Felix.startBundle(Felix.java:2045)[org.apache.felix.framework-4.4.1.jar:] 
    at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:976)[org.apache.felix.framework-4.4.1.jar:] 
    at org.apache.felix.fileinstall.internal.DirectoryWatcher.startBundle(DirectoryWatcher.java:1245)[9:org.apache.felix.fileinstall:3.5.0] 
    at org.apache.felix.fileinstall.internal.DirectoryWatcher.startBundles(DirectoryWatcher.java:1217)[9:org.apache.felix.fileinstall:3.5.0] 
    at org.apache.felix.fileinstall.internal.DirectoryWatcher.doProcess(DirectoryWatcher.java:509)[9:org.apache.felix.fileinstall:3.5.0] 
    at org.apache.felix.fileinstall.internal.DirectoryWatcher.process(DirectoryWatcher.java:358)[9:org.apache.felix.fileinstall:3.5.0] 
    at org.apache.felix.fileinstall.internal.DirectoryWatcher.run(DirectoryWatcher.java:310)[9:org.apache.felix.fileinstall:3.5.0] 

... 

問題: 如何在「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"); 
    } 
} 

aaa.bbb.ccc .Processor1.java

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()); 
    } 
}; 

駱駝route.xml

<?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> 

的pom.xml

<?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:[email protected]> 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:[email protected]> 

包含deploy目錄IBM MQ OSGi的罐子

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) 

謝謝您的幫助!

回答

1

您所遇到的部署錯誤是因爲你沒有com.fasterxml.jackson.databind罐子的類路徑的正確版本(如下定義)。您是否在使用功能文件進行部署?如果是這樣,請將其納入您的問題中。

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

你的方式使用處理器反序列化爲JSON是正確的,但是我建議在Process()方法之外創建ObjectMapper(),因爲每次都不需要新的ObjectMapper(),創建一個代價很高。

+0

感謝java-addict301!說得通。 - 我會在這個週末嘗試一下,並以身份回覆你! – sairn

+0

未使用「功能文件」進行部署。安裝了「camel-jackson」(即「camel-2.17.0.redhat-630187」)。注意:我已經爲IBM MQ 8.x添加了OSGI捆綁包(到部署文件夾)。這些捆綁可能會導致某種衝突嗎? – sairn

+1

最終想通了,謝謝你的建議 - thx。不得不查找什麼駱駝傑克遜的紅帽版本使用...發現它在這個頁面上(https://mvnrepository.com/artifact/org.apache.camel/camel-jackson/2.17.0.redhat-630283 )。然後改變了pom.xml的依賴關係一樣: 「 com.fasterxml.jackson.core傑克遜,數據綁定 2.6.3」 – sairn