我需要下载哪个Jersey版本才能与JDK 1.9兼容?

7

我正在使用jdk 1.9版本,尝试启动一个简单的Rest服务。使用Maven原型创建了该项目: jersey-quickstart-webapp。然后修改了pom.xml和web.xml文件。我的Maven配置如下所示:

<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/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>restWebService</groupId>
    <artifactId>restWebService</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>restWebService</name>

    <build>
        <finalName>restWebService</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <inherited>true</inherited>
                <configuration>
                    <source>9</source>
                    <target>9</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.glassfish.jersey</groupId>
                <artifactId>jersey-bom</artifactId>
                <version>${jersey.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.0.0.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.sun.jersey/jersey-client -->
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-client</artifactId>
            <version>1.19.4</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.sun.jersey/jersey-bundle -->
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-bundle</artifactId>
            <version>1.19.4</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.sun.jersey/jersey-server -->
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-server</artifactId>
            <version>1.19.4</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.sun.jersey/jersey-core -->
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-core</artifactId>
            <version>1.19.4</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/asm/asm -->
        <dependency>
            <groupId>org.ow2.asm</groupId>
            <artifactId>asm</artifactId>
            <version>6.0_BETA</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.json/json -->
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20170516</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-impl -->
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.3.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/javax.xml/jaxb-api -->
        <dependency>
            <groupId>javax.xml</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/javax.activation/activation -->
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1.1</version>
        </dependency>

        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-json</artifactId>
            <version>1.19.4</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-core -->
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-core</artifactId>
            <version>2.3.0</version>
        </dependency>

    </dependencies>
    <properties>
        <jersey.version>2.16</jersey.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

而 web.xml 如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
     see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
        <servlet-name>Jersey REST Service</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>restWebService</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey REST Service</servlet-name>
        <url-pattern>/webapi/*</url-pattern>
    </servlet-mapping>
</web-app>

当我尝试访问这个URL:http://localhost:8080/webapi/myresource,会出现以下错误:
HTTP Status 500 – Internal Server Error

Type Exception Report

Message Servlet.init() for servlet [Jersey REST Service] threw exception

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

javax.servlet.ServletException: Servlet.init() for servlet [Jersey REST Service] threw exception
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:475)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:651)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
    org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:500)
    org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:754)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1376)
    org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
    java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.base/java.lang.Thread.run(Thread.java:844)
Root Cause

java.lang.IllegalArgumentException
    jersey.repackaged.org.objectweb.asm.ClassReader.<init>(ClassReader.java:170)
    jersey.repackaged.org.objectweb.asm.ClassReader.<init>(ClassReader.java:153)
    jersey.repackaged.org.objectweb.asm.ClassReader.<init>(ClassReader.java:424)
    com.sun.jersey.spi.scanning.AnnotationScannerListener.onProcess(AnnotationScannerListener.java:138)
    com.sun.jersey.core.spi.scanning.uri.FileSchemeScanner$1.f(FileSchemeScanner.java:86)
    com.sun.jersey.core.util.Closing.f(Closing.java:71)

当我将构建配置从1.9更改为1.8时

<configuration>
    <source>1.8</source>
    <target>1.8</target>
</configuration>

服务没有任何问题就启动了,我在浏览器上看到了预期的结果。

我正在使用Intellij,项目SDK-9,项目语言级别-9。

为了使其与1.9一起工作,我应该导入哪些jar包?或者我需要更改maven配置才能使其正常工作?

编辑:依赖树

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building restWebService 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[WARNING] The artifact javax.xml:jaxb-api:jar:2.1 has been relocated to javax.xml.bind:jaxb-api:jar:2.1
[INFO] 
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ restWebService ---
[WARNING] The artifact javax.xml:jaxb-api:jar:2.1 has been relocated to javax.xml.bind:jaxb-api:jar:2.1
[INFO] restWebService:restWebService:war:1.0-SNAPSHOT
[INFO] +- org.springframework:spring-core:jar:5.0.0.RELEASE:compile
[INFO] |  \- org.springframework:spring-jcl:jar:5.0.0.RELEASE:compile
[INFO] +- com.sun.jersey:jersey-client:jar:1.19.4:compile
[INFO] +- com.sun.jersey:jersey-bundle:jar:1.19.4:compile
[INFO] |  \- javax.ws.rs:jsr311-api:jar:1.1.1:compile
[INFO] +- com.sun.jersey:jersey-server:jar:1.19.4:compile
[INFO] +- com.sun.jersey:jersey-core:jar:1.19.4:compile
[INFO] +- org.ow2.asm:asm:jar:6.0_BETA:compile
[INFO] +- org.json:json:jar:20170516:compile
[INFO] +- com.sun.xml.bind:jaxb-impl:jar:2.3.0:compile
[INFO] +- javax.xml.bind:jaxb-api:jar:2.1:compile
[INFO] |  \- javax.xml.stream:stax-api:jar:1.0-2:compile
[INFO] +- javax.activation:activation:jar:1.1.1:compile
[INFO] +- com.sun.jersey:jersey-json:jar:1.19.4:compile
[INFO] |  +- org.codehaus.jettison:jettison:jar:1.1:compile
[INFO] |  +- org.codehaus.jackson:jackson-core-asl:jar:1.9.2:compile
[INFO] |  +- org.codehaus.jackson:jackson-mapper-asl:jar:1.9.2:compile
[INFO] |  +- org.codehaus.jackson:jackson-jaxrs:jar:1.9.2:compile
[INFO] |  \- org.codehaus.jackson:jackson-xc:jar:1.9.2:compile
[INFO] \- com.sun.xml.bind:jaxb-core:jar:2.3.0:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.910 s
[INFO] Finished at: 2017-10-14T22:05:15-04:00
[INFO] Final Memory: 13M/44M
[INFO] ------------------------------------------------------------------------
4个回答

2

目前的Jersey存在着一个有阴影的asm 5依赖性问题,导致无法与Java 9字节码一起使用。看起来Jersey 1.x没有适应Java 9,也不清楚是否会提供这种支持。作为临时解决方案,我创建了一个分支,在其中删除了阴影的asm,在代码中迁移了包以使用普通的asm依赖项,并将asm 6.0放入Maven依赖项中。

您可以从此分支中构建仅包含jersey-server和jersey-servlet模块,因为仅这两个模块依赖于asm并使用这两个自定义工件在您的项目中。

我的项目使用这种替换很好,但我不能保证它是100%的解决方案,我未能构建整个分发而没有失败,并且现在无法投资时间进行全面采用和检查。同时,主要的Jersey部分构建没有任何问题。

因此,您最终的pom文件如下所示:

<!--regular jersey dependencies with the last 1.19.4 version-->
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-server</artifactId>
    <version>${custom-jersey-with-asm6.version}</version>
</dependency>

<dependency>
     <groupId>com.sun.jersey</groupId>
     <artifactId>jersey-servlet</artifactId>
     <version>${custom-jersey-with-asm6.version}</version>
</dependency>

<dependency>
     <groupId>org.ow2.asm</groupId>
     <artifactId>asm</artifactId>
     <version>6.0</version>
</dependency>

1

有很多工件可能需要更新。首先,您应该检查模块的 mvn dependency:tree,以查找任何带入版本 < 6.0.xasm 源,因为它与 Java9 不兼容。从当前的 pom.xml 和执行日志中推断,您可以进行更新。

<!-- https://mvnrepository.com/artifact/asm/asm -->
<dependency>
    <groupId>asm</groupId>
    <artifactId>asm</artifactId>
    <version>3.3.1</version>
</dependency>

to

<dependency>
    <groupId>org.ow2.asm</groupId>
    <artifactId>asm</artifactId>
    <version>6.0</version>
</dependency>

重要的是,使用 Java 9兼容的Maven插件,例如 -

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.7.0</version>
    <inherited>true</inherited>
    <configuration>
        <source>9</source>
        <target>9</target>
    </configuration>
</plugin>

顺便提一下,你可能仍然会面临其他挑战(我可以看到冲突的jaxb依赖项,甚至asm可能是从jersey传递而来),需要解决这些问题你应该运行

mvn dependency:analyze

并确保在这种情况下排除冲突的依赖项,以利用它们的更新版本。

谢谢您的建议。但是在将您的更改合并到pom.xml后,我仍然看到相同的错误。 - RDM
让我现在来做。 - RDM
我已更新我的答案,包括依赖树。asm从6.0_BETA被导入。 - RDM
是的,那就是完整的依赖树。我已经更新了我的问题,并附上了整个pom.xml文件。同时在问题中添加了以下内容:“使用Maven原型创建了该项目:jersey-quickstart-webapp。然后我继续修改了pom.xml和web.xml文件。” - RDM
5
当ASM试图读取一个版本比它所理解的更新的类文件时,会抛出IllegalArgumentException异常。因此,我认为评论是正确的方向。但是,如果你仔细查看栈帧,例如jersey.repackaged.org.objectweb.asm.ClassReader.<init>,那么看起来Jersey已经重新打包了ASM。因此,我认为您需要找到一个包含ASM 6(用于解析版本为53.0的类文件)的Jersey版本。 - Alan Bateman
显示剩余4条评论

0
我遇到了这个错误,但与Jersey的版本无关。它是由于一些弹性搜索jar文件引起的,即(elastic-search、elastic-search-core和log4j.2.1.1)。这些多版本jar文件有一个名为“version”的文件夹,其中包含一些在META-INF中的工件。我删除了该文件夹,问题得到解决。

0
问题在于Jersey 1.x系列的最高支持版本只到Java 8。
如果你尝试在Java 9或以上版本中运行Jersey 1.x,你会因为兼容性而出错。
原因是Jersey服务器中ClassReader类中的Opcodes.V1_8。
 public ClassReader(final byte[] b, final int off, final int len) {
        this.b = b;
        // checks the class version
        if (readShort(off + 6) > Opcodes.V1_8) {
            throw new IllegalArgumentException();

因此,在Opcodes.V1_8中,V1_8代表Java类文件版本1.8的常量值,对应于Java SE 8。换句话说,它检查正在读取的类文件是否具有大于Java SE 8的类文件版本,如果是,则会抛出IllegalArgumentException异常。

因此,结论是您需要使用jersey 2.x系列来支持上述版本。


1
你的回答可以通过补充支持性信息来改进。请进行[编辑]以添加更多详细信息,例如引用或文档,以便其他人可以确认您的回答是正确的。您可以在帮助中心中找到有关撰写良好答案的更多信息。 - Community

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接