我一直在用jersey和java构建我自己的REST API。今天,它停止工作,但在一个朋友也在推动他的改变。他没有改变任何依赖关系,但是他添加了一个我们的主API类创建的控制器。现在,每当我尝试访问资源时,tomcat服务器都会抛出一个错误:
exception
javax.servlet.ServletException: Servlet execution threw an exception
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.lang.AbstractMethodError: javax.ws.rs.core.UriBuilder.uri(Ljava/lang/String;)Ljavax/ws/rs/core/UriBuilder;
javax.ws.rs.core.UriBuilder.fromUri(UriBuilder.java:119)
com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:669)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.ja
我们认为它是在他添加jsoup依赖之后开始的。
编辑:我编辑了我的依赖项和web.xml,现在我只得到404没有找到。
这是我的pom.xml中的依赖项
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.23.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.23.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.codehaus.jettison</groupId>
<artifactId>jettison</artifactId>
<version>1.3.8</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>org.twitter4j</groupId>
<artifactId>twitter4j-core</artifactId>
<version>[4.0,)</version>
</dependency>
<dependency>
<groupId>org.facebook4j</groupId>
<artifactId>facebook4j-core</artifactId>
<version>[2.4,)</version>
</dependency>
</dependencies>
This is my web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>api</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
</web-app>
编辑如果我尝试访问:http://localhost:8080/api-mashup-api/api/v1/foobar
@Path("/v1")
public class API {
private Controller controller;
public API(){
controller = new Controller();
}
/**
* Prints to the screen if we are in /v1/foobar
*
* @return
*/
@GET
@Path("/foobar")
@Produces(MediaType.TEXT_HTML)
public String print2() {
return "Please specify what resource you need.";
}
结果是404
可能是由于错误的jersey-container-servlet导致错误的uribuilder被拾取
https://jersey.java.net/documentation/latest/modules-and-dependencies.html server-jdk
Jersey -container- Servlet => Jersey core Servletx实现
Jersey -container- Servlet -core => Jersey core Servletx实现
改变:
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.23.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.23.2</version>
<scope>provided</scope>
</dependency>
还可以参考https://jersey.java.net/documentation/latest/modules-and-dependencies.html#dependencies来了解您需要为Glassfish和其他基于Servlet(非jax-rs集成)的容器提供哪些依赖项
1)当应用程序试图调用抽象方法时抛出AbstractMethodError
异常uri
是UriBuilder
中的抽象方法,因此需要对其进行实现。
您应该在Jersey 2中使用JAX-RS 2.0。*实现了JAX-RS 2.0并包含了uri
方法的实现。
2)通过查看您的堆栈跟踪,它清楚地表明您正在使用Jersey版本1和amp;2,这是不可能的,所以类加载器拾取错误的URIBuilder类。
堆栈跟踪:
com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:669)
组com.sun.jersey
中的Jersey依赖项都是Jersey版本1。Jersey版本2使用org.glassfish.jersey
组