Java RestFull WebService:使用 Jersey 2.3.1 库的 JAX-RS 实现

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19225618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 15:13:50  来源:igfitidea点击:

Java RestFull WebService: JAX-RS implementation with Jersey 2.3.1 libraries

javajerseyjax-rsjboss6.x

提问by Roberto

I am trying to run a simple "Hallo World" application Jersey 2.3.1 REST service on JBoss jboss-eap-6.1 AS. In web.xml i have disabled restEasy library. During deployment i am getting the error:

我正在尝试在 JBoss jboss-eap-6.1 AS 上运行一个简单的“Hallo World”应用程序 Jersey 2.3.1 REST 服务。在 web.xml 中,我禁用了 restEasy 库。在部署期间我收到错误:

JBWEB000289: Servlet com.sun.jersey.samples.helloworld.resources.MyApplication threw load() exception: java.lang.NoSuchMethodError: javax.ws.rs.core.Application.getProperties()Ljava/util/Map;

JBWEB000289:Servlet com.sun.jersey.samples.helloworld.resources.MyApplication 抛出 load() 异常:java.lang.NoSuchMethodError:javax.ws.rs.core.Application.getProperties()Ljava/util/Map;

In POM i put these dependencies:

在 POM 中,我放置了这些依赖项:

<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-server</artifactId>
    <version>2.3.1</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet-core</artifactId>
    <version>2.3.1</version>
</dependency>
<dependency>
    <groupId>javax.ws.rs</groupId>
    <artifactId>javax.ws.rs-api</artifactId>
    <version>2.0</version>
</dependency>

This is my web.xml with restEasy tags disabling:

这是我的 web.xml,禁用了 restEasy 标签:

<?xml version="1.0" encoding="UTF-8"?>
<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>com.sun.jersey.samples.helloworld.resources.MyApplication</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>com.sun.jersey.samples.helloworld.resources.MyApplication</param-value>
        </init-param>
           <load-on-startup>1</load-on-startup>
    </servlet>
    <context-param>
        <param-name>resteasy.scan</param-name>
        <param-value>false</param-value>
    </context-param>
    <context-param>
        <param-name>resteasy.scan.providers</param-name>
        <param-value>false</param-value>
    </context-param>
    <context-param>
        <param-name>resteasy.scan.resources</param-name>
        <param-value>false</param-value>
    </context-param>
    <servlet-mapping>
        <servlet-name>com.sun.jersey.samples.helloworld.resources.MyApplication</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

And my resource config java class:

和我的资源配置 java 类:

package com.sun.jersey.samples.helloworld.resources;
import org.glassfish.jersey.server.ResourceConfig;
public class MyApplication extends  ResourceConfig {   

     public MyApplication() {
            packages("com.sun.jersey.samples.helloworld.resources");
          //super(HelloWorldResource.class);

     }
}

Someone have any idea to solve it? thanks in advance, Roberto

有人有任何想法来解决它吗?提前致谢,罗伯托

采纳答案by AdamL

NoSuchMethodErrorusually means you are having two different versions of the class on your classpath. As the javax.ws.rs.core.Applicationclass does have the getProperties()method in its JAX-RS 2 version, but not in JAX-RS 1.x, I would guess that somehow you are combining the old 1.x Jersey (or old REST api) with the current (2.3.1) one.

NoSuchMethodError通常意味着您的类路径上有两个不同版本的类。由于javax.ws.rs.core.Application该类getProperties()在其 JAX-RS 2 版本中确实具有该方法,但在 JAX-RS 1.x 中没有,我猜想您以某种方式将旧的 1.x Jersey(或旧的 REST api)与当前的(2.3 .1) 一。

Also the package you are working in (com.sun.jersey- the 'old' Jersey package) points a bit towards this direction (although just placing your code into that package itself cannot cause the mentioned problem), you obviously started with the Jersey 1.x example as a base (there are samples in Jersey 2 as well, see helloworld-webappon Jersey GitHub).

此外,您正在使用的包(com.sun.jersey- '旧' Jersey 包)有点指向这个方向(尽管将您的代码放入该包本身不会导致上述问题),您显然从 Jersey 1.x 示例开始一个基础(在 Jersey 2 中也有示例,请参阅Jersey GitHub 上的helloworld-webapp)。

Is it possible, that restEasy (also definitely containing the javax.ws.rs.core.Applicationclass) is not completely switched off and somehow defaults to JAX-RS 1.x version?

有没有可能,restEasy(也肯定包含javax.ws.rs.core.Application该类)没有完全关闭并且以某种方式默认为 JAX-RS 1.x 版本?

I would start with inspecting your pom file, look at the effective pom (if your project descriptor has some parent) and check carefully what is on your classpath - I believe there is a 1.x version of javax.ws.rs-apisomewhere. Also try to clean all the compiled stuff and rebuild from scratch.

我将首先检查您的 pom 文件,查看有效的 pom(如果您的项目描述符有一些父项)并仔细检查您的类路径上的内容 - 我相信javax.ws.rs-api某处有 1.x 版本。还尝试清理所有已编译的内容并从头开始重建。

Speaking of dependencies, if your list is exhaustive (regarding Jersey), you will most likely have to add jersey-common(2.3.1) dependency, as already during the initialization, the ResourceConfig.packages()method calls the PackageScannerconstructor, which contains call to ReflectionHelper- and this is not a part of the server jar any more.

说到依赖项,如果您的列表是详尽的(关于 Jersey),您很可能必须添加jersey-common(2.3.1) 依赖项,因为在初始化期间,该ResourceConfig.packages()方法调用PackageScanner构造函数,其中包含对ReflectionHelper- 的调用,这不是不再是服务器 jar 的一部分。

Hope this helps.

希望这可以帮助。

回答by Jan Snelders

Just got this working on JBoss EAP 6.1.1 - Jersey 2.3.1.

刚刚在 JBoss EAP 6.1.1 - Jersey 2.3.1 上运行。

The usual things don't seem to work/are not enough on their own:

通常的事情似乎不起作用/本身还不够:

  • disabling the jaxrs-subsystem in standalone.xml/domain.xml
  • or, excluding the jax-rs modules in jboss-deployment-structure.xml
  • 在 standalone.xml/domain.xml 中禁用 jaxrs 子系统
  • 或者,排除 jboss-deployment-structure.xml 中的 jax-rs 模块

Additionaly you need to disable the loading of the jax-rs 1.1 API completely by modifying module.xml in jboss-eap-6.1/modules/system/layers/base/javax/ws/rs/api/main/module.xml like this:

另外,您需要通过像这样修改 jboss-eap-6.1/modules/system/layers/base/javax/ws/rs/api/main/module.xml 中的 module.xml 来完全禁用 jax-rs 1.1 API 的加载:

<module xmlns="urn:jboss:module:1.1" name="javax.ws.rs.api">
<resources>
    <!-- Disable the next line -->
    <!-- resource-root path="jboss-jaxrs-api_1.1_spec-1.0.1.Final-redhat-2.jar"/ -->
    <!-- Insert resources here -->
</resources>

<dependencies>
    <module name="org.jboss.resteasy.resteasy-jaxrs" services="export"/>
</dependencies>
</module>

Please note that this will disable the jax-rs implementation of JBoss (RestEasy) for all other applications as well (as does disabling the jaxrs subsystem in standalone/domain.xml).

请注意,这也会为所有其他应用程序禁用 JBoss (RestEasy) 的 jax-rs 实现(就像禁用 standalone/domain.xml 中的 jaxrs 子系统一样)。

回答by gihan-maduranga

I faced same problem recently. I thought share my steps for you. As the other answers state, the problem is mainly because of having two different versions of the same class on your classpath. So when you add maven dependencies in your pom be careful.

我最近遇到了同样的问题。我想为你分享我的步骤。正如其他答案所述,问题主要是因为您的类路径上有同一个类的两个不同版本。所以当你在你的 pom 中添加 maven 依赖项时要小心。

These kind of problems are normally called as Jar Hell. You can use jhadesAPI for investigate the classes overlap one another. Here is the simple steps i have followed.

这类问题通常被称为Jar Hell。您可以使用jhadesAPI 来调查彼此重叠的类。这是我遵循的简单步骤。

Add jhades dependency into your pom.

将 jhades 依赖项添加到您的 pom.xml 文件中。

<dependency>
    <groupId>org.jhades</groupId>
    <artifactId>jhades</artifactId>
    <version>1.0.4</version>
</dependency>

Show the report

显示报告

Call new JHades().overlappingJarsReport();in your mainmethod, it will output to stdout.

调用new JHades().overlappingJarsReport();您的main方法,它将输出到标准输出。

Sample Output:

示例输出:

file:/Users/justin/.m2/repository/javax/ws/rs/jsr311-api/1.1.1/jsr311-api-1.1.1.jar overlaps with
file:/Users/justin/.m2/repository/javax/ws/rs/javax.ws.rs-api/2.0/javax.ws.rs-api-2.0.jar - total overlapping classes: 55 - same classloader ! This is an ERROR!

Remove one of overlap maven dependency in your pom.

删除 pom 中的重叠 maven 依赖项之一。

Also you can use another approach like maven's dependency exclusions.

您也可以使用另一种方法,例如 maven's dependency excludes

Source: Blog post on jhades

来源:关于 jhades 的博客文章

Hope this will help someone :)

希望这会帮助某人:)

回答by Hajar Homayouni

This is a Jersey version conflict problem. I had the same problem. Here is how it is resolved:

这是一个 Jersey 版本冲突问题。我有同样的问题。这是它的解决方法:

  1. See your package dependencies "mvn dependency:tree"

  2. If there is a library dependency that depends on an old Jersey version, you could add an exclusions section in the dependency tag for that library in pom.xml

  1. 查看您的包依赖项“mvn dependency:tree”

  2. 如果存在依赖于旧 Jersey 版本的库依赖项,您可以在 pom.xml 中该库的依赖项标记中添加一个排除部分

回答by David Loy

Using mvn dependency:tree (thanks for suggestion above) I was able to identify that the culprit (in my case) was: javax.ws.rs:jsr311-api:1.1 Removing this dependency solved my problem.

使用 mvn dependency:tree (感谢上面的建议)我能够确定罪魁祸首(就我而言)是: javax.ws.rs:jsr311-api:1.1 删除此依赖项解决了我的问题。