Igo*_*orM 5 java rest spring tomcat jax-rs
我正在尝试公开REST-full服务(由Tomcat托管),并且无法弄清楚Spring 3(M3)所需的配置是什么.
这是(示例)服务的样子:
@Controller
@Scope("prototype")
public class UsersController
{
@RequestMapping(value="/users/hello", method=RequestMethod.GET)
public String hello()
{
return "hello, user!";
}
}
Run Code Online (Sandbox Code Playgroud)
我的Spring配置看起来像这样(为简单起见,我省略了完整的类名):
<beans ...>
<context:annotation-config />
<bean class="...AutowiredAnnotationBeanPostProcessor"/>
<bean class="...DefaultAnnotationHandlerMapping">
<context:component-scan base-package="com.mycompany.myserver"/>
</beans>
Run Code Online (Sandbox Code Playgroud)
这就是我将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>
Run Code Online (Sandbox Code Playgroud)
请注意,我正在尝试创建一个最小配置(没有额外的servlet-config.xml文件).这就是为什么我将Dispatcher指向内置配置.
它是否正确?
在我启动Tomcat并加载所有bean之后,我将导航到以下URL:
http://localhost:8080/myserver/services/users/hello
Run Code Online (Sandbox Code Playgroud)
并且,而不是"你好,用户!" 回复,令我沮丧的是,我在日志文件中看到以下错误:
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
Run Code Online (Sandbox Code Playgroud)
如您所知,我当前的配置是尝试将此请求重新路由为新请求:
/myserver/services/users/hello, user!
Run Code Online (Sandbox Code Playgroud)
知道什么是错的吗?我错过了什么?
问题是你的 hello() 方法被注释为 Spring 的 handlerMethod ,并且根据 @RequestMapping 文档,处理程序方法可以返回任意数量的对象,但如果它返回 String 那么:
一个字符串值,被解释为视图名称,模型通过命令对象和 ModelAttribute 带注释的引用数据访问器方法隐式确定。处理程序方法还可以通过声明 ModelMap 参数以编程方式丰富模型(见上文)
所以它基本上是在寻找您返回“你好,用户”的字符串的映射 - 该映射不存在。您可以让您的方法返回 void 并在方法内部自行写入 HttpServletResponse。
进一步回答:
@RequestMapping(value="/users/hello", method=RequestMethod.GET)
public void hello(Writer w) {
w.write("hello, user!");
w.flush();
return;
}
Run Code Online (Sandbox Code Playgroud)
您可能想要获取整个 HttpServletResponse 而不仅仅是 Writer,这样您就可以设置适当的 Http Headers。
抱歉,我下面的评论很混乱,我是这个网站的新手。
归档时间: |
|
查看次数: |
2812 次 |
最近记录: |