jersey 和 Jackson 的 Maven 依赖性问题?

发布于 2024-11-18 01:26:50 字数 10235 浏览 2 评论 0 原文

我刚刚开始使用 jersey 和 jackson 构建一个网络应用程序。最初启动并运行后,我认为从长远来看,将项目转换为 Maven 项目是有意义的。我对 Maven 很陌生,似乎我可能遇到了球衣和杰克逊之间的依赖问题。

在我的一些使用 jackson 的项目测试遇到问题后,我整理了一个简单的测试来检查一切是否都已正确设置。用户对象基于 jackson 教程 中的对象

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

}

,该对象抛出以下异常

 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

我尝试运行相同的在一个简单的 Maven Java 应用程序中进行测试,以确保我设置 Jackson 依赖项的方式没有问题并且一切正常。我怀疑这个问题是由球衣和杰克逊之间的一些依赖性问题引起的,因为它也使用杰克逊。我还怀疑我将网络应用程序的 pom 文件拼凑在一起的方式可能会出现问题。这是我的 pom 的内容,有一些轻微的命名更改(正如我之前所说,我刚开始使用 Maven,所以我会说这是一个非常难看的 pom 文件)

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

有谁知道导致杰克逊问题的原因以及球衣是否存在依赖性问题以及如何解决?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

铁轨上的流浪者 2024-11-25 01:26:50

正如已经提到的,这可能是版本冲突(错误消息确实表明某些内容是针对 1.8 编译的,但以某种方式使用了早期版本)。

我注意到导致问题的一件事是“核心”和“映射器”jar 的版本可能不同。例如,在本例中,听起来使用的是映射器版本 1.8,但核心 jar 版本更早。尽管 Jackson core jar 确实定义了正确的版本,但 Maven 可能不依赖于该信息,而是依赖于版本某些其他组件的要求。

因此,每当从您自己的 pom.xml 指定对 Jackson 的依赖时,请确保核心 (jackson-asl-core) 和映射器 (jackson-ask-mapper) 的版本均已定义,并且具有相同的值(或至少具有相同的次要版本) ,1.8.x)。

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

忆离笙 2024-11-25 01:26:50

升级 datanucleus-hbase 库后,我遇到了同样的问题。

我的解决方案是包含两个杰克逊库:

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

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>
辞别 2024-11-25 01:26:50

jersey-json 依赖项有其自己的 jackson 依赖项。

对于 Jersey 1.8,其为 Jackson 1.7.1。因此,首先删除 1.8.1 和 1.8.2 依赖项,它们可能会造成麻烦。 Maven 不是确定性的:如果它遇到同一依赖项的两个版本(具有相同的计数),则无法预测所选择的版本。

您不必创建 JSON 字符串,也不必自己调用 Jackson 映射器。看看我在 GitHub 上发布的这个完全正常工作/经过测试的应用。希望它会有所帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文