简体   繁体   English

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

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

I am trying to run a simple "Hallo World" application Jersey 2.3.1 REST service on JBoss jboss-eap-6.1 AS. 我试图在JBoss jboss-eap-6.1 AS上运行一个简单的“Hallo World”应用程序Jersey 2.3.1 REST服务。 In web.xml i have disabled restEasy library. 在web.xml中我禁用了restEasy库。 During deployment i am getting the error: 在部署期间,我收到错误:

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 罗伯托,提前谢谢

NoSuchMethodError usually means you are having two different versions of the class on your classpath. NoSuchMethodError通常意味着您在类路径上有两个不同版本的类。 As the javax.ws.rs.core.Application class 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. 由于javax.ws.rs.core.Application类在其JAX-RS 2版本中确实具有getProperties()方法,但在JAX-RS 1.x中没有,我猜想你会以某种方式组合旧的1.x泽西(或旧的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-webapp on Jersey GitHub). 你正在使用的软件包( com.sun.jersey - '旧'Jersey软件包)指向这个方向(尽管只是将代码放入该软件包本身不会导致上述问题),你显然是从泽西开始的1.x示例作为基础(在Jersey 2中也有样本,请参阅Jersey GitHub上的helloworld-webapp )。

Is it possible, that restEasy (also definitely containing the javax.ws.rs.core.Application class) 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-api somewhere. 我将首先检查你的pom文件,查看有效的pom(如果你的项目描述符有一些父级)并仔细检查你的类路径是什么 - 我相信有一个1.x版本的javax.ws.rs-api Also try to clean all the compiled stuff and rebuild from scratch. 还尝试清理所有已编译的东西并从头开始重建。

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 PackageScanner constructor, 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. 希望这可以帮助。

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 . 这些问题通常称为Jar Hell You can use jhades API for investigate the classes overlap one another. 您可以使用jhades API来调查类彼此重叠。 Here is the simple steps i have followed. 这是我遵循的简单步骤。

Add jhades dependency into your pom. 将jhades依赖项添加到您的pom中。

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

Show the report 显示报告

Call new JHades().overlappingJarsReport(); 调用new JHades().overlappingJarsReport(); in your main method, it will output to stdout. main方法中,它将输出到stdout。

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的依赖项排除

Source: Blog post on jhades 来源: 关于jhades的博客文章

Hope this will help someone :) 希望这会帮助别人:)

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 禁用standalone.xml / domain.xml中的jaxrs-subsystem
  • or, excluding the jax-rs modules in jboss-deployment-structure.xml 或者,不包括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子系统)。

This is a Jersey version conflict problem. 这是泽西岛版本的冲突问题。 I had the same problem. 我有同样的问题。 Here is how it is resolved: 以下是解决方法:

  1. See your package dependencies "mvn dependency:tree" 查看包依赖项“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 如果存在依赖于旧版Jersey的库依赖项,则可以在pom.xml中为该库的依赖项标记添加排除项部分

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM