2017-04-20 71 views
0

我試圖編寫REST服務並使用Tomcat啓動它。我web.xml是:部署在Tomcat上的Jersey應用程序返回404

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
    version="3.0"> 

    <display-name>TomcatRestExample</display-name> 

    <servlet> 
     <servlet-name>Jersey Web Application</servlet-name> 
     <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> 
     <init-param> 
       <param-name>jersey.config.server.provider.packages</param-name> 
       <param-value>de.continentale.testsvn.rest</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>Jersey Web Application</servlet-name> 
     <url-pattern>/maven/*</url-pattern> 
    </servlet-mapping> 

</web-app> 

de.continentale.testsvn.rest.RestCaller類的樣子:

package de.continentale.testsvn.rest; 

import javax.ws.rs.GET; 
import javax.ws.rs.Path; 
import javax.ws.rs.Produces; 
import javax.ws.rs.core.MediaType; 

@Path("/service") 
public class RestCaller { 

    @GET 
    @Produces(MediaType.TEXT_XML) 
    public String convert() { 
     return "<text>hallo</text>"; 
    } 
} 

我試圖訪問http://localhost:8090/TomcatRestExample/maven/service但得到:在Eclipse

HTTP Status 404 - /TomcatRestExample/maven/service 

type Status report 

message /TomcatRestExample/maven/service 

description The requested resource is not available. 

Apache Tomcat/7.0.41 

Tomcat的控制檯輸出:

INFORMATION: Reloading Context with name [/tomcat-rest-test] has started 
Apr 20, 2017 2:23:19 PM com.sun.jersey.api.core.servlet.WebAppResourceConfig init 
INFORMATION: Scanning for root resource and provider classes in the Web app resource paths: 
    /WEB-INF/lib 
    /WEB-INF/classes 
Apr 20, 2017 2:23:19 PM com.sun.jersey.api.core.ScanningResourceConfig logClasses 
INFORMATION: Root resource classes found: 
    class de.continentale.testsvn.rest.RestCaller 
Apr 20, 2017 2:23:19 PM com.sun.jersey.api.core.ScanningResourceConfig init 
INFORMATION: No provider classes found. 
Apr 20, 2017 2:23:19 PM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate 
INFORMATION: Initiating Jersey application, version 'Jersey: 1.19 02/11/2015 05:39 AM' 
Apr 20, 2017 2:23:20 PM org.apache.catalina.core.StandardContext reload 
INFORMATION: Reloading Context with name [/tomcat-rest-test] is completed 

任何想法我失蹤?

+0

'localhost:8090/TomcatRestExample'工作嗎? – aUserHimself

+0

控制檯上的任何其他錯誤?你有什麼依賴? –

+0

@aUserHimself localhost:8090/TomcatRestExample也回答相同的迴應。我需要一個html頁面來完成這項工作嗎? –

回答

2

看起來你的上下文不正確。您的應用程序部署在/tomcat-rest-test而不是在/TomcatRestExample。後者只是管理工具使用的顯示名稱。 Tomcat使用WAR文件名稱(不含.war)作爲上下文名稱。

所以用http://localhost:8090/tomcat-rest-test/maven/service來打你的資源方法。

1

看來你的背景是/tomcat-rest-test而不是/TomcatRestExample。嘗試第一個:localhost:8090/tomcat-rest-test/maven/service

相關問題