簡體   English   中英

創建我自己的 REST API 會引發 servlet 執行異常

[英]Creating my own REST API throws servlet execution exception

我一直在使用 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 not found。

這是我的依賴項形成我的 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>

這是我的 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 核心 Servlet 3.x 實現

jersey-container-servlet-core => Jersey 核心 Servlet 2.x 實現

改變:

<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異常,當應用程序嘗試調用抽象方法時會拋出該異常。
uriUriBuilder的抽象方法,因此您需要實現它。

您應該將 JAX-RS 2.0 與 Jersey 2.* 一起使用,后者實現 JAX-RS 2.0 並包含對uri方法的實現。

2)通過查看您的堆棧跟蹤,它清楚地表明您同時使用 Jersey 版本 1 和 2,這是不可能的,因此類加載器選擇了錯誤的 URIBuilder 類。

堆棧跟蹤:

com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:669)

com.sun.jersey組中的 Jersey 依賴項都是 Jersey 版本 1。 Jersey 版本 2 使用組org.glassfish.jersey

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM