【问题标题】:What Jersey version do I need to download for JDK 1.9?我需要为 JDK 1.9 下载什么 Jersey 版本?
【发布时间】:2021-06-22 16:41:37
【问题描述】:

我正在使用 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

我应该导入哪些 jar 才能使其与 1.9 一起使用?还是我需要更改 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] ------------------------------------------------------------------------

【问题讨论】:

    标签: java rest jersey jax-rs java-9


    【解决方案1】:

    当前 Jersey 存在着色 asm 5 依赖项的问题,该依赖项不适用于 Java 9 字节码。看起来 Jersey 1.x 没有被 Java 9 采用,目前还不清楚这种支持是否会到来。作为一个临时解决方案,我创建了一个branch,在那里我删除了阴影 asm,在代码中迁移了包以使用常规的 asm 依赖项并将 asm 6.0 放入 maven 依赖项中。

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

    我的项目在使用这样的替代品时运行良好,但我不能保证它是 100% 有效的解决方案,我无法毫无失败地构建整个发行版,也无法投入时间全面采用并检查正确性现在。同时,泽西岛的主要部件也没有问题。

    所以,你的最终 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>
    

    【讨论】:

      【解决方案2】:

      您可能需要更新很多工件。首先,您应该检查您的模块的mvn dependency:tree,以寻找任何引入版本&lt; 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>
      

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

      重要的是,使用Java 9 compatible maven 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>
      

      另一方面,您可能最终仍面临一些其他挑战(我可以看到冲突的 jaxb 依赖项,甚至 asm 可能从 jersey 传递)来解决您应该运行的问题

      mvn dependency:analyze
      

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

      【讨论】:

      • 感谢您的建议。但是在将您的更改合并到 pom.xml 后,我看到了同样的错误
      • @RahulDevMishra 您是否尝试弄清楚依赖关系树没有从答案中提到的任何其他来源带来asm
      • 让我现在就做
      • 我已经用依赖树更新了我的答案。 asm 正在从 6.0_BETA 导入
      • ASM 在被要求读取其可以理解的较新版本的类文件时抛出 llegalArgumentException。所以我认为cmets走在正确的轨道上。但是,如果您仔细查看堆栈帧 jersey.repackaged.org.objectweb.asm.ClassReader.&lt;init&gt; ,那么看起来 Jersey 已经重新打包了 ASM。所以我认为你需要找到一个包含 ASM 6 的 Jersey 版本(需要解析 53.0 版本的类文件)。
      【解决方案3】:

      我遇到了这个错误,就我而言,它与泽西岛的版本无关。这是由于一些弹性搜索 jar 即(elastic-search、elastic-search-core 和 log4j.2.1.1)是多版本 jar。多版本 jar 有一个名为 version 的文件夹,其中包含 META-INF 中的一些工件。我删除了那个文件夹,它解决了这个问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-05-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-28
        • 1970-01-01
        相关资源
        最近更新 更多