Spring REST实现错误404

时间:2013-11-27 14:45:52

标签: java spring rest spring-mvc http-status-code-404

我正在尝试使用here中的教程开发测试REST应用程序。我将它作为一场战争部署,将战争包含在我的pom中,并构建了没有错误的应用程序。在打开网址时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类看起来像这样:

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-dispatcher-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:

有人能帮我在这里找出问题吗?

1 个答案:

答案 0 :(得分:1)

您未指定contextConfig位置

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

缺少参数值。请提供mvc-dispatcher-servlet.xml的位置,然后重试。希望这有帮助