3

I have just started building a web app using jersey and jackson. After initially getting up and running I decided that it made sense in the long run to convert the project to be a maven one. I'm very new to maven and it seems like I might have run into a dependency issue between jersey and jackson.

I put together a simple test to check that everything had been set up correctly after running into issues with some of my project tests that use jackson. The user object is based on the one in the jackson tutorial

@Test
public void testJacksonSetup() throws IOException {
    String json = "{\"name\":{\"first\":\"Joe\",\"last\":\"Sixpack\"},\"verified\":false,\"gender\":\"MALE\",\"userImage\":\"Rm9vYmFyIQ==\"}";
    ObjectMapper mapper = new ObjectMapper();
    User user =  mapper.readValue(json, User.class);
    assertEquals("Joe", user.getName().getFirst());
    Writer strWriter = new StringWriter();
    mapper.writeValue(strWriter, user);
    assertEquals(json, strWriter.toString());

}

which is throwing the following exception

 org.codehaus.jackson.type.JavaType.isMapLikeType()Z
 java.lang.NoSuchMethodError: org.codehaus.jackson.type.JavaType.isMapLikeType()Z
    at org.codehaus.jackson.map.ser.BasicSerializerFactory.buildContainerSerializer(BasicSerializerFactory.java:396)
    at org.codehaus.jackson.map.ser.BasicSerializerFactory.buildContainerSerializer(BasicSerializerFactory.java:396)
    at org.codeh

I tried running the same test in a simple maven java application to make sure there weren't issues with the way I had set up the jackson dependencies and everything worked. I suspect the problem is caused by some dependency issue between jersey and jackson since it also uses jackson. I also suspect that way I cobbled have the pom file for my web app together could be problem. Here is the content of my pom with some slight naming changes (as I said before I'm new to using maven so I'd say this is a pretty ugly pom file)

<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>
<parent>
    <groupId>com.myproject.app</groupId>
    <artifactId>app</artifactId>
    <version>0.1-SNAPSHOT</version>
</parent>
<groupId>com.myproject.app</groupId>
<version>${project.parent.version}</version>
<artifactId>my-web-app</artifactId>
<name>My web app</name>
<packaging>war</packaging>
<properties>
    <netbeans.hint.deploy.server>gfv3</netbeans.hint.deploy.server>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <jersey-version>1.8-SNAPSHOT</jersey-version>
</properties>
<profiles>
    <profile>
        <id>default</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <dependencies>
            <dependency>
                <groupId>com.myproject.app</groupId>
                <artifactId>myproject-core</artifactId>
                <version>${project.parent.version}</version>
            </dependency>
            <dependency>
                <groupId>com.sun.jersey</groupId>
                <artifactId>jersey-server</artifactId>
                <version>${jersey-version}</version>
                <!--<scope>provided</scope>-->
            </dependency>
            <dependency>
                <groupId>com.sun.jersey</groupId>
                <artifactId>jersey-json</artifactId>
                <version>${jersey-version}</version>
                <!--<scope>provided</scope>-->
            </dependency>
            <dependency>
                <groupId>com.sun.jersey.contribs</groupId>
                <artifactId>jersey-multipart</artifactId>
                <version>${jersey-version}</version>
                <!--<scope>provided</scope>-->
            </dependency>
            <dependency>
                <groupId>com.sun.jersey.jersey-test-framework</groupId>
                <artifactId>jersey-test-framework-grizzly2</artifactId>
                <version>${jersey-version}</version>
                <scope>test</scope>
            </dependency>
            <!-- for external testing -->
            <dependency>
                <groupId>com.sun.jersey.jersey-test-framework</groupId>
                <artifactId>jersey-test-framework-external</artifactId>
                <version>${jersey-version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.codehaus.jackson</groupId>
                <artifactId>jackson-mapper-asl</artifactId>
                <version>1.8.2</version>
            </dependency>
            <dependency>
                <groupId>org.codehaus.jackson</groupId>
                <artifactId>jackson-xc</artifactId>
                <version>1.8.1</version>
            </dependency>
        </dependencies>
    </profile>
</profiles>
<build>
    <finalName>mywebapp</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.1.1</version>
            <configuration>
                <packagingExcludes>WEB-INF/glassfish-web.xml</packagingExcludes>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <inherited>true</inherited>
            <version>2.3.2</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
        <!-- Run the application using "mvn embedded-glassfish:run" -->
        <plugin>
            <groupId>org.glassfish</groupId>
            <artifactId>maven-embedded-glassfish-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <goalPrefix>embedded-glassfish</goalPrefix>
                <app>${basedir}/target/mywebapp.war</app>
                <autoDelete>true</autoDelete>
                <port>8080</port>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>com.sun.jersey</groupId>
                    <artifactId>jersey-server</artifactId>
                    <version>1.7</version>
                </dependency>
                <dependency>
                    <groupId>org.glassfish</groupId>
                    <artifactId>javax.servlet</artifactId>
                    <version>3.1</version>
                </dependency>
                <dependency>
                    <groupId>org.glassfish</groupId>
                    <artifactId>javax.ejb</artifactId>
                    <version>3.1</version>
                </dependency>
            </dependencies>
        </plugin>
        <plugin>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>maven-jetty-plugin</artifactId>
            <version>6.1.24</version>
            <configuration>
                <webApp>${basedir}/target/mywebapp.war</webApp>
                <contextPath>treemetrics-api-01</contextPath>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>xslt-maven-plugin</artifactId>
            <version>1.0</version>
            <executions>
                <execution>
                    <id>update-gf-deps</id>
                    <phase>package</phase> <!-- append to the packaging phase. -->
                    <goals>
                        <goal>transform</goal> <!-- goals == mojos -->
                    </goals>
                    <configuration>
                        <xslFile>src/main/xslt/gf.xsl</xslFile>
                        <srcDir>.</srcDir>
                        <srcIncludes>pom.xml</srcIncludes>
                        <destDir>target/gf-pom-file</destDir>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
<pluginRepositories>
    <pluginRepository>
        <id>maven2-repository.dev.java.net</id>
        <name>Java.net Repository for Maven</name>
        <url>http://download.java.net/maven/2/</url>
        <layout>default</layout>
    </pluginRepository>
    <pluginRepository>
        <id>maven2-glassfish-repository.dev.java.net</id>
        <name>Java.net Repository for Maven</name>
        <url>http://download.java.net/maven/glassfish/</url>
    </pluginRepository>
</pluginRepositories>
<repositories>
    <repository>
        <id>glassfish-repository</id>
        <name>Java.net Repository for Glassfish</name>
        <url>http://download.java.net/maven/glassfish</url>
    </repository>
    <repository>
        <id>maven2-repository.dev.java.net</id>
        <name>Java.net Repository for Maven</name>
        <url>http://download.java.net/maven/2/</url>
        <layout>default</layout>
    </repository>
</repositories>

Does anyone know what is causing the issues with jackson and If there is a dependency issue with jersey and how to resolve it?

3 Answers 3

3

As already mentioned, this is probably a version conflict (error message does suggest that something is compiled against 1.8, but an earlier version is being used somehow).

One thing that I have noticed to cause issues is that versions of "core" and "mapper" jars may differ. In this case, for example, it sounds like mapper version 1.8 was being used, but core jar version was earlier. Although Jackson core jar does define proper version, Maven may not rely on that information but by version some other component mandates.

So whenever specifying dependency to Jackson from your own pom.xml, make sure that versions of core (jackson-asl-core) and mapper (jackson-ask-mapper) are both defined, and have same value (or at least same minor versions, 1.8.x).

3
  • Thanks you so much! I was tearing my hair out. I added an entry in my pom for jackson-asl-core and my tests all passed. I'm guessing that in my simple application the reference to jackson-asl-mapper caused the dependency jackson-asl-core to be pulled down correctly but in my application including jackson a version of jackson-asl-core was detected by maven as already being present; it ignored the versioning somehow and the conflict emerged. Does this sound like makes sense, I'm just trying to get a sense of how maven operates?
    – Conor
    Jul 1, 2011 at 9:07
  • Yes, I think this is about how it goes. I, too, am trying to learn more about Maven; and handling of transitive dependencies with version numbers is not a simple task in itself. But I suspect that precedence for versions to use is partly based on "closest match"; so whichever dependency is closest to your pom.xml is chosen. Finally, if Maven worked better with version ranges this might not be such a problem; but as things are, most poms have to specify exact version, which is usually becomes stale quite soon (Maven can support ranges, but in my experience it just Does Not Work Well :-/ )
    – StaxMan
    Jul 1, 2011 at 16:56
  • ah okay. Thanks once again for your help and explanations, they really helped me out
    – Conor
    Jul 5, 2011 at 11:25
3

I was having the same issue after I upgraded the datanucleus-hbase lib.

My solution was to include the two jackson libs:

        <dependency>
        <groupId>org.datanucleus</groupId>
        <artifactId>datanucleus-hbase</artifactId>
        <version>3.1.0-m1</version>
    </dependency>

    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-core-asl</artifactId>
        <version>1.9.4</version>
    </dependency>

    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.4</version>
    </dependency>
1

The jersey-json dependencies have its own jackson dependency.

For Jersey 1.8 its Jackson 1.7.1. So, start by removing the 1.8.1 and 1.8.2 dependencies, they may cause trouble. Maven is not deterministic: if it encounters two versions of the same dependency (with the same count), the chosen one cannot be predicted.

You don't have to create a JSON string neither to call Jackson mapper yourself. Take a look a this fully working/tested app I've post on GitHub. Hope it will help.

4
  • Hi yves, thans for your reply. Unfortunately removing those dependencies has no effect. I should also clarify that the error is being thrown during the marshalling mapper.writeValue(strWriter, user);
    – Conor
    Jun 30, 2011 at 16:33
  • I am using JsonContextNatural().createJSONMarshaller().marshallToJSON(object, System.out) Jun 30, 2011 at 16:56
  • the github link is broken
    – neves
    Aug 4, 2017 at 19:24
  • @neves sorry, I've deleted the repository but, hopefully, someone forked me then. I've updated the link accordingly ;-) Aug 22, 2017 at 12:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.