Java RestFull WebService:使用Jersey 2.3.1库的JAX-RS实现 [英] Java RestFull WebService: JAX-RS implementation with Jersey 2.3.1 libraries
问题描述
我正在尝试在JBoss jboss-eap-6.1 AS上运行一个简单的Hallo World应用程序Jersey 2.3.1 REST服务。在web.xml中我禁用了restEasy库。在部署期间,我收到错误:
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:
JBWEB000289:Servlet
com.sun.jersey.samples.helloworld.resources.MyApplication抛出load()
异常:java.lang.NoSuchMethodError:
javax.ws.rs.core.Application.getProperties()Ljava / util / Map;
JBWEB000289: Servlet com.sun.jersey.samples.helloworld.resources.MyApplication threw load() exception: java.lang.NoSuchMethodError: javax.ws.rs.core.Application.getProperties()Ljava/util/Map;
在POM中我放了这些依赖项:
In POM i put these dependencies:
<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>
这是我的web.xml,其中restEasy标签禁用:
This is my web.xml with restEasy tags disabling:
<?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>
我的资源配置java类:
And my resource config java class:
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);
}
}
有人有任何想法解决它?
提前感谢,
Roberto
Someone have any idea to solve it? thanks in advance, Roberto
推荐答案
NoSuchMethodError
通常意味着您在类路径上有两个不同版本的类。由于 javax.ws.rs.core.Application
类在其JAX-RS中具有 getProperties()
方法2版本,但不是在JAX-RS 1.x中,我猜你会把旧的1.x Jersey(或旧的REST api)与当前的(2.3.1)结合起来。
NoSuchMethodError
usually means you are having two different versions of the class on your classpath. 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.
您正在使用的套餐( com.sun.jersey
- '旧'泽西套餐)点有点朝这个方向发展(虽然只是将你的代码放入该包中本身不会引起上述问题),你显然是以Jersey 1.x为例开始的(在Jersey 2中也有样本,见helloworld-webapp 。
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).
是否有可能,restEasy(也肯定包含 javax.ws.rs.core.Application
类)未完全关闭,并以某种方式默认为JAX-RS 1。 x版本?
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?
我首先检查你的pom文件,查看有效的pom(如果你的项目描述符有一些父级)并仔细检查你的类路径上的内容 - 我相信某处有一个1.x版本的 javax.ws.rs-api
。还尝试清理所有已编译的东西并从头开始重建。
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. Also try to clean all the compiled stuff and rebuild from scratch.
说到依赖关系,如果你的列表是详尽的(关于Jersey),你很可能必须添加 jersey-common
(2.3.1)依赖,就像在初始化期间一样, ResourceConfig.packages()
方法调用 PackageScanner
构造函数,其中包含对 ReflectionHelper
的调用 - 而且这不再是服务器jar的一部分。
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.
希望这会有所帮助。
这篇关于Java RestFull WebService:使用Jersey 2.3.1库的JAX-RS实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!