2013-11-27 96 views
3

我正在嘗試使用here的教程來開發測試REST應用程序。我把它作爲一場戰爭來部署,通過在我的戰爭中包括戰爭,並且構建了沒有錯誤的應用程序。當涉及到打開網址,localhost:8080/gs-rest-service/rest/greeting我得到一個404錯誤 - 更具體地是No mapping found for HTTP request with URI [/gs-rest-service/rest/greeting] in DispatcherServlet with name 'mvc-dispatcher'。我似乎無法看到什麼是錯的,我檢查了我的上下文根設置爲gs-rest-service,我GreetingController類看起來是這樣的:Spring REST實現錯誤404

package hello; 

import java.util.concurrent.atomic.AtomicLong; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestParam; 
import org.springframework.web.bind.annotation.ResponseBody; 

@Controller 
public class GreetingController { 

private static final String template = "Hello, %s!"; 
private final AtomicLong counter = new AtomicLong(); 

@RequestMapping("/greeting") 
public @ResponseBody Greeting greeting(
     @RequestParam(value="name", required=false, defaultValue="World") String name) { 
    return new Greeting(counter.incrementAndGet(), 
         String.format(template, name)); 
} 

}

這裏是我的web.xml:

<web-app id="WebApp_ID" version="2.4" 
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 

<display-name>Spring Web MVC Application</display-name> 

<servlet> 
    <servlet-name>mvc-dispatcher</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value></param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>mvc-dispatcher</servlet-name> 
    <url-pattern>/rest/*</url-pattern> 
</servlet-mapping> 

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value> 
</context-param> 

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

這裏是我的MVC-調度-servlet.xml中

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation=" 
    http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 

<context:component-scan base-package="home" /> 

<mvc:annotation-driven /> 
</beans> 

This is my project layout:

會有人能夠幫助我在這裏找到問題?

+0

請張貼您的啓動日誌。 –

+0

[Tom REST風格的服務作爲WAR而不是JAR在Tomcat中可能的重複](http://stackoverflow.com/questions/19820029/spring-restful-service-as-a-war-instead-of-jar-in- tomcat) –

+0

@SotiriosDelimanolis日誌位於哪裏? – AkshaiShah

回答

1

你沒有指定contextConfig位置

<init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value></param-value> 
    </init-param> 

PARAM價值的缺失。請給出mvc-dispatcher-servlet.xml的位置,然後再試一次。希望這可以幫助

+0

位置是mvc-servlet-dispatcher? – AkshaiShah

+0

這將是你的mvc-dispatcher-servlet.xml – Keerthivasan

+0

剛剛看到這個,試試吧 – AkshaiShah