6

我正在尝试公开一个完整的 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!

知道有什么问题吗?我错过了什么?

4

1 回答 1

1

问题是您的 hello() 方法被注释为 Spring 的 handlerMethod 并且根据 @RequestMapping 文档,处理程序方法可能会返回任意数量的对象,但如果它返回 String 那么:

@Request 文档

一个字符串值,它被解释为视图名称,模型通过命令对象和 ModelAttribute 注释的引用数据访问器方法隐式确定。处理程序方法还可以通过声明 ModelMap 参数以编程方式丰富模型(见上文)

所以它基本上是在寻找你返回 'hello, user' 的那个字符串的映射——它不存在。您可能让您的方法返回 void 并在方法内部自己写出 HttpServletResponse。

进一步回答:

@RequestMapping(value="/users/hello", method=RequestMethod.GET) 
public void hello(Writer w) { 
   w.write("hello, user!"); 
   w.flush(); 
   return; 
} 

您可能想要获取整个 HttpServletResponse 而不仅仅是 Writer,这样您就可以设置适当的 Http Headers。

对不起,我下面的评论很混乱,我是这个网站的新手。

于 2009-05-12T22:40:27.343 回答