我正在尝试公开一个完整的 REST 服务(由 Tomcat 托管),但无法弄清楚 Spring 3 (M3) 所需的配置是什么。
这就是(示例)服务的样子:
@Controller
@Scope("prototype")
public class UsersController
{
@RequestMapping(value="/users/hello", method=RequestMethod.GET)
public String hello()
{
return "hello, user!";
}
}
我的 Spring 配置如下所示(为简单起见,我省略了完整的类名):
<beans ...>
<context:annotation-config />
<bean class="...AutowiredAnnotationBeanPostProcessor"/>
<bean class="...DefaultAnnotationHandlerMapping">
<context:component-scan base-package="com.mycompany.myserver"/>
</beans>
这就是我将 Spring 配置插入 web.xml 的方式:
<listener>
<listener-class>...RequestContextListener</listener-class>
</listener>
<!-- Servlets -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>...DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:*:dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
请注意,我正在尝试创建一个最小配置(没有额外的 servlet-config.xml 文件)。这就是我将 Dispatcher 指向内置配置的原因。
这个对吗?
启动 Tomcat 并加载所有 bean 后,我将导航到以下 URL:
http://localhost:8080/myserver/services/users/hello
而且,而不是“你好,用户!” 回复,令我沮丧的是,我在日志文件中看到以下错误:
09:54:45,140 DEBUG RequestContextListener:69 - Bound request context to thread: org.apache.catalina.connector.RequestFacade@19299f5
09:54:45,140 DEBUG DispatcherServlet:834 - DispatcherServlet with name 'dispatcher' determining Last-Modified value for [/myserver/services/users/hello]
09:54:45,156 DEBUG DefaultAnnotationHandlerMapping:178 - Mapping [/users/hello] to handler 'com.symantec.repserver.myserver.UsersController@21447f'
09:54:45,171 DEBUG DispatcherServlet:850 - Last-Modified value for [/myserver/services/users/hello] is: -1
09:54:45,171 DEBUG DispatcherServlet:683 - DispatcherServlet with name 'dispatcher' processing GET request for [/myserver/services/users/hello]
09:54:45,218 DEBUG HandlerMethodInvoker:148 - Invoking request handler method: public java.lang.String com.symantec.repserver.myserver.UsersController.hello()
09:54:45,218 DEBUG DefaultListableBeanFactory:1366 - Invoking afterPropertiesSet() on bean with name 'hello, user!'
09:54:45,218 DEBUG DispatcherServlet:1060 - Rendering view [org.springframework.web.servlet.view.InternalResourceView: name 'hello, user!'; URL [hello, user!]] in DispatcherServlet with name 'dispatcher'
09:54:45,234 DEBUG InternalResourceView:237 - Forwarding to resource [hello, user!] in InternalResourceView 'hello, user!'
09:54:45,250 DEBUG DispatcherServlet:834 - DispatcherServlet with name 'dispatcher' determining Last-Modified value for [/myserver/services/users/hello, user!]
09:54:45,250 DEBUG DispatcherServlet:842 - No handler found in getLastModified
09:54:45,250 DEBUG DispatcherServlet:683 - DispatcherServlet with name 'dispatcher' processing GET request for [/myserver/services/users/hello, user!]
09:54:45,250 WARN PageNotFound:959 - No mapping found for HTTP request with URI [/myserver/services/users/hello, user!] in DispatcherServlet with name 'dispatcher'
09:54:45,250 DEBUG DispatcherServlet:643 - Successfully completed request
09:54:45,250 DEBUG DispatcherServlet:643 - Successfully completed request
09:54:45,250 DEBUG RequestContextListener:89 - Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@19299f5
如您所见,我当前的配置正在尝试将此请求重新路由为新请求:
/myserver/services/users/hello, user!
知道有什么问题吗?我错过了什么?