我有一个奇怪的场景,除非我将调度程序 servlet 映射到 web.xml 中的 /*,否则不会调用我的控制器。我已经定义了一个带有 RequestMapping 的控制器:
@Controller
public class UserController {
@RequestMapping(value = "/rest/users", method = RequestMethod.GET)
public ModelAndView getUsers(HttpServletRequest request) throws RestException {
...
}
}
和一个应用程序上下文:
<?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" />
最后在 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>
这按预期工作,即我可以向/rest/users 发出请求。但是,如果我将 web.xml 映射更改为:
<servlet-mapping>
<servlet-name>rest-servlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
我收到一个 MVC 错误:
WARN servlet.PageNotFound:在名称为“rest-servlet”的 DispatcherServlet 中找不到具有 URI [/rest/users] 的 HTTP 请求的映射。
这看起来很奇怪,因为错误表明请求被映射到 dispatcher-servlet,但唯一改变的是 servlet 映射。
有人遇到过这种情况么?