java Spring MVC @RequestMapping 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10250631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:14:13  来源:igfitidea点击:

Spring MVC @RequestMapping not working

javaspring-mvc

提问by Anton

I have a strange scenario in which my controller is not invoked unless I map the dispatcher servlet to /* in web.xml. I have defined a controller with a RequestMapping:

我有一个奇怪的场景,除非我将调度程序 servlet 映射到 web.xml 中的 /*,否则不会调用我的控制器。我已经定义了一个带有 RequestMapping 的控制器:

@Controller  
public class UserController {

    @RequestMapping(value = "/rest/users", method = RequestMethod.GET)
    public ModelAndView getUsers(HttpServletRequest request) throws RestException {
      ...
    }  
}

And an application context:

和一个应用程序上下文:

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

    <context:component-scan base-package="com.test.rest.controller" /> 

Finally this is mapped in web.xml:

最后在 web.xml 中映射:

<servlet>
    <servlet-name>rest-servlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/restContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

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

This works as expected i.e. I can make requests to /rest/users. However if I change the web.xml mapping to:

这按预期工作,即我可以向 /rest/users 发出请求。但是,如果我将 web.xml 映射更改为:

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

I get an MVC error:

我收到一个 MVC 错误:

WARN servlet.PageNotFound: No mapping found for HTTP request with URI [/rest/users] in DispatcherServlet with name 'rest-servlet'.

警告 servlet.PageNotFound:在 DispatcherServlet 中找不到名称为“rest-servlet”的带有 URI [/rest/users] 的 HTTP 请求的映射。

It seems really strange because the error indicates that the request is being mapped to the dispatcher-servlet, yet the only thing that has changed is the servlet mapping.

这看起来真的很奇怪,因为错误表明请求被映射到调度程序 servlet,但唯一改变的是 servlet 映射。

Has anyone else encountered this?

有人遇到过这种情况么?

回答by Anton

Dispatcher servlet is the main servlet of Spring MVC. It handle all request, coming to your application, and using its own routing engine dispatch it to Controllers. If you change it to

Dispatcher servlet 是 Spring MVC 的主要 servlet。它处理所有请求,到达您的应用程序,并使用自己的路由引擎将其分派给控制器。如果你把它改成

 <url-pattern>/rest/*</url-pattern>

Then your request should be like this rest/rest/users

那么你的请求应该是这样的 rest/rest/users

The common pattern - allow dispatch servlet to handle all incoming request (first configuration is valid)

通用模式——允许 dispatch servlet 处理所有传入的请求(第一个配置有效)