Conor
Conor

Reputation: 775

jersey and jackson maven dependency issues?

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?

Upvotes: 3

Views: 16531

Answers (3)

yves amsellem
yves amsellem

Reputation: 7234

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.

Upvotes: 1

Rafael Sanches
Rafael Sanches

Reputation: 1823

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>

Upvotes: 3

StaxMan
StaxMan

Reputation: 116482

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).

Upvotes: 3

Related Questions