【问题标题】:404 Error in @RequestMapping(value = "/projects", method = RequestMethod.GET)@RequestMapping 中的 404 错误(值 =“/projects”,方法 = RequestMethod.GET)
【发布时间】:2014-02-26 08:04:23
【问题描述】:

我用 STS 创建了一个 Spring MVC 项目。在这个项目中,我有带有 @RequestMapping("/") 的默认 HomeController。该控制器被正确调用。

那我还有这个控制器

package de.gl.rm;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import de.gl.rm.model.Project;

@Controller
public class Projects {

    @RequestMapping(value = "/projects", method = RequestMethod.GET)
    public @ResponseBody List<Project> getProjects() {
        Project p1 = new Project(1, "P1");
        Project p2 = new Project(2, "P2");

        ArrayList<Project> list =  new ArrayList<Project>();
        list.add(p1);
        list.add(p2);
        return list;
    }
}

但是在 /projects 我的 webapp 没有响应。我的配置是这样的

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/js/**" location="/resources/js" />
<resources mapping="/css/**" location="/resources/css" />
<resources mapping="/img/**" location="/resources/img" />
<resources mapping="/resources/**" location="/resources" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>

<context:component-scan base-package="de.gl.rm" />
<context:component-scan base-package="de.gl.rm.controller" />

我不知道为什么 /projects 不起作用。有人有想法吗?

谢谢

【问题讨论】:

  • 你如何使用 /projects 使用 ajax?
  • 不,来自地址栏的浏览器请求。
  • 然后删除 ResponseBody 注释,您应该从 Get 方法返回视图名称。
  • 我总是想检索 json
  • 然后通过 AJAX 调用并在您的视图中呈现数据。

标签: spring spring-mvc


【解决方案1】:

我可以给你部分答案:

1- 删除&lt;context:component-scan base-package="de.gl.rm.controller" /&gt;,因为&lt;context:component-scan base-package="de.gl.rm" /&gt; 将扫描de.gl.rm 以及子包。

2- 删除

<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <beans:property name="prefix" value="/WEB-INF/views/" />
  <beans:property name="suffix" value=".jsp" />
</beans:bean>

因为您有一个@ResponseBody(通常用于处理网页内的一些 AJAX 请求)所以它只是在您请求 localhost:8080/rm/projects 时打印的 List&lt;Project&gt; 不需要视图解析器,但您需要一个内容协商,如果您希望 JSON 添加 produces = {"application/json"} 并添加 lib 以在您的 pom 中完成这项工作,例如 jackson。

3- 你的 web.xml 需要配置一个 servlet 调度器:

 <servlet>
   <servlet-name>mvc-dispatcher</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 </servlet>
 <servlet-mapping>
   <servlet-name>mvc-dispatcher</servlet-name>
   <url-pattern>/rm/*</url-pattern>
 </servlet-mapping>

url-pattern 使用 rm,因为您的 url 是 localhost:8080/rm/projects(假设您的上下文路径是 /)否则它将是 localhost:8080/&lt;context-path&gt;/rm/projects

4- 我建议你只使用注释进行配置(没有任何 xml 和 web.xml 为空)

【讨论】:

  • 我不建议删除视图解析器,它很可能在其他地方使用
  • 是的,当然,如果我们只使用@ResponseBody,这只是为了理解,但你说得对,它肯定在其他地方使用过。
【解决方案2】:

记录的输出到底是什么,有很多问题,但没有一个会导致 404...

您正在返回一个 reposnsebody,因此它将返回一个项目列表,格式为 json 或 xml。但是,由于它是一个自定义类,您需要提供一个转换器/序列化器。

将 mvc-annotation 驱动添加到您的配置中,这将为您提供默认的序列化器/转换器(可能会立即与项目类的 toString 方法一起使用,但不确定)。

【讨论】:

    【解决方案3】:

    使用

    xmlns:mvc="http://www.springframework.org/schema/mvc"
    
    <mvc:annotation-driven />
    

    在您的 servlet xml 中。

    当我尝试在我的控制器中使用 BindingResults 来检查表单的结果对于我的数据模型是否有效时,这为我解决了 404 问题(例如:不接受一个人的空名称) .

    希望这能解决您的问题:)

    【讨论】:

      猜你喜欢
      • 2017-09-18
      • 1970-01-01
      • 2017-05-31
      • 1970-01-01
      • 1970-01-01
      • 2020-11-27
      • 1970-01-01
      • 2016-12-28
      • 2016-03-11
      相关资源
      最近更新 更多