How can I successfully use the Jackson "ObjectMapper()" inside a "process()" method

2k views Asked by At

PROBLEM: using Jackson's "ObjectMapper()" within the "process()" method causes an exception:

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

The exception is shown in the Jboss Fuse console, it looks like this:

...

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]

...

How can I successfully use the Jackson "ObjectMapper()" inside a "process()" method, i.e., to convert a map into json?


To provide context, below is the info on this simple application/bundle:

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

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

Camel features installed:

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>

Deploy directory containing IBM MQ osgi jars:

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

Other environment info:

jdk1.8.0_131
jboss-fuse-6.3.0.redhat-187
WebLogic 12.2.1  (running the rest service)
1

There are 1 answers

4
java-addict301 On BEST ANSWER

The deploy error you are encountering is because you don't have the correct version of the com.fasterxml.jackson.databind jar on the classpath (as defined below). Are you using a features file for deployment? If so, please include it in your question.

org.osgi.framework.BundleException: Unresolved constraint in bundle aaa.bbb.ccc.camelRest [585]: Unable to resolve 585.0: missing requirement [585.0] osgi.wiring.package; (&(osgi.wiring.package=com.fasterxml.jackson.databind)(version>=2.7.0)(!(version>=3.0.0)))

The way you are using Processor to deserialize to JSON is correct, however I would suggest creating the ObjectMapper() outside of the Process() method since you don't need a new one each time and creating one is expensive.