java - Spring REST 实现错误 404

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

我正在尝试使用 here 中的教程开发测试 REST 应用程序.我通过在我的 pom 中包含 war 将其部署为 war ,并且构建了没有错误的应用程序。在打开 URL 时,localhost:8080/gs-rest-service/rest/greeting 我收到 404 错误 - 更具体地说是 No mapping found for HTTP request with URI [/gs-rest-service/rest/greeting] 在 DispatcherServlet 中,名称为“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:

谁能帮我找出这里的问题?

最佳答案

您没有指定 contextConfig 位置

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

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

关于java - Spring REST 实现错误 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20245466/

相关文章:

java - hibernate 外键

java - 如何为多线程 Java 服务器实现观察者设计模式?

javascript - 在 Javascript 中验证响应来 self 的服务器的最佳方法?

r - Shiny 的服务器。打印 JSON 作为结果输出

java - 当选择 jtextfield 或按钮时如何保持 jframe 的焦点

java - 无法为我的公历设置所需的时间

java - 没有 jcl-over-slf4j 的 Spring + Logback(slf4j) 日志记录

rest - 是否可以在没有 IIS 的情况下安装和使用 OpenRasta?如果是这样,怎么办?

java - Spring基于java的配置中非公共(public)类的使用

java - Spring appConfig.java 类如何工作