2016-08-14 83 views
0

我在eclipse中使用Jersey配置了一個REST應用程序。當在web.xml中配置路徑時,REST API返回404 NOT FOUND

當web.xml中的路徑配置爲/*,但我將其更改爲/rest/*時,我收到404 NOT FOUND錯誤。 在服務器上沒有例外。

web.xml文件,如下所示:

<servlet> 
    <servlet-name>Jersey Web Application</servlet-name> 
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> 
    <init-param> 
     <param-name>jersey.config.server.provider.packages</param-name> 
     <param-value>com.app.user</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
    <servlet-name>Jersey Web Application</servlet-name> 
    <url-pattern>/rest/*</url-pattern> 
</servlet-mapping> 
<welcome-file-list> 
    <welcome-file>index.html</welcome-file> 
</welcome-file-list> 

這裏是我宣佈Java文件

@Path("/rest/products") 
    public class Product { 

我得到一個404錯誤的路徑時,我訪問服務器URL上的路徑/休息/產品。

我錯過了什麼?

非常感謝幫助!

回答

1

當您將Jersey Web Application映射到/rest/*時,所有請求的路徑中應該有/rest。您將Product類映射到/rest/products,因此整個url應爲http://localhost:port/contextRoot/rest/rest/products。如果您不想在URL中休息兩次,請將Product類映射到/products

0

你已經將你的servlet映射到「/ rest/*」 url,即每當有一個URL的請求時......./rest/*你的servlet將調用ServletContainer來處理它。

您的java文件@Path(「/ rest/products」)被提及。

由於@Path中提到的路徑中存在正斜槓,因此您將收到此404錯誤。這是因爲當你給路徑開始正斜槓時,它將其視爲絕對路徑而不是相對路徑。

所以您的最終網址不會落得像/myProject的/ REST /產品而是會像/REST /產品它無法找到。

因此錯誤。

相關問題