ディスパッチャ サーブレットを 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 要求のマッピングが見つかりません。
エラーはリクエストがディスパッチャーサーブレットにマップされていることを示しているため、本当に奇妙に思えますが、変更されたのはサーブレットのマッピングだけです。
他の誰かがこれに遭遇しましたか?