【问题标题】:Spring REST Implementation Error 404Spring REST 实现错误 404
【发布时间】:2013-12-13 06:31:24
【问题描述】:

我正在尝试使用来自here 的教程开发一个测试 REST 应用程序。我将它部署为战争,通过在我的 pom 中包含战争,并且构建了没有错误的应用程序。在打开 URL 时,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>

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

【问题讨论】:

  • 请发布您的启动日志。
  • @SotiriosDelimanolis 日志在哪里?
  • No mapping found for HTTP request with URI [/gs-rest-service/rest/greeting] in DispatcherServlet with name 'mvc-dispatcher' 相同的地方。
  • 服务方法不应该用\@GET、\@POST等注释吗? AFAIK,您尚未将 HTTP 方法与您的服务相关联。

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


【解决方案1】:

您没有指定 contextConfig 位置

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

参数值丢失。请给出 mvc-dispatcher-servlet.xml 的位置,然后重试。希望这会有所帮助

【讨论】:

  • 位置会是 mvc-servlet-dispatcher 吗?
  • 这将是你的 mvc-dispatcher-servlet.xml
  • 啊刚看到这个,试试看
猜你喜欢
  • 1970-01-01
  • 2021-12-18
  • 2019-02-20
  • 2015-04-24
  • 2017-03-15
  • 2016-06-22
  • 2020-04-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多