2016-07-22 89 views
0

當試圖在用戶資源上調用GET方法時,出現上述錯誤消息:http://localhost:8080/Trempiada/users/12345。 或者只需輸入項目的主要URI:http://localhost:8080/Trempiada。 我有Spring的偵聽器DI和tomcat服務器加載沒有任何異常。 這裏是我的web.xml文件:請求的資源不可用(HTTP 404) - Jersey和Spring集成

<?xml version="1.0" encoding="UTF-8"?> 
<!-- This web.xml file is not required when using Servlet 3.0 container, 
    see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html --> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> 

     <!-- Configure ContextLoaderListener to use JavaConfigWebApplicationContext 
     instead of the default XmlWebApplicationContext --> 
    <context-param> 
     <param-name>contextClass</param-name> 
     <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value> 
    </context-param> 

     <!-- Configuration locations must consist of one or more comma- or space-delimited 
     fully-qualified @Configuration classes --> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>org.sharon.trempiada.resources.ResourcesConfiguration org.sharon.trempiada.services.ServicesConfiguration</param-value> 
    </context-param> 

    <listener> 
     <listener-class> 
      org.springframework.web.context.ContextLoaderListener 
     </listener-class> 
    </listener> 

    <servlet> 
     <servlet-name>trempiada</servlet-name> 
     <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> 
     <init-param> 
      <param-name>jersey.config.server.provider.packages</param-name> 
      <param-value>org.sharon.trempiada.resources</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>trempiada</servlet-name> 
    <url-pattern>/*</url-pattern> 
    </servlet-mapping> 
</web-app> 

我應該檢查並在可能是問題?

回答

0

Tomcat中web應用程序的上下文路徑默認基於war文件名。所以,Trempiada的上下文路徑意味着戰爭文件名是Trempiada-1.0.war。你能證實這是這種情況嗎?如果沒有,您需要調整您的網址以包含實際的上下文路徑。 Tomcat通常在啓動過程中記錄此信息。

第二部分是資源端點。爲了配合您所提供的網址,你需要的資源類,如下所示:

@Path("users") 
public class UserResource { 
    ... 
    @GET 
    @Produces("application/json") 
    @Path("{id:\\d+}") 
    public User getUser() { 
     // Return user object. 
    } 
} 

如果你這樣做,你就可以在http://localhost:8080/Trempiada/users/12345訪問您的端點。

+0

解決了我的問題。我沒有注意到它是區分大小寫的(Trempiada!= trempiada),我認爲上下文基於webapp名稱,就像在eclipse中的「Servers/Tomcat」標籤下顯示的一樣。 – sharon182

相關問題