1、有UserController 代码如下:
1 @Controller
2 @RequestMapping("/")
3 public class UserController {
4 @RequestMapping(value = "{userid}", method = RequestMethod.GET)
5 public ModelAndView userIndex(@PathVariable Long userid){
6 return new ModelAndView("userIndex").addObject(userid);
7 }
8 }
2、web.xml中的两处DispatcherServlet配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 classpath:spring/hessian-remoting.xml
17
18
19
20
21
22
23
24
3、hessian-remoting.xml中服务配置:
1
2
3
4
4、我预期的结果是:
当我在浏览器中直接访问hessian服务“http://localhost/hessian/userHessianService”时,应该报错:HTTP Status 405 - HessianServiceExporter only supports POST requests
虽然报错,但可以表明这个请求被hessian服务接收到了。
而实际情况是:报错为 HTTP Status 400 - The request sent by the client was syntactically incorrect ()
5、问题出在哪里?把web.xml中的user-servlet注释掉,重新启动并访问,能得到预期结果,加上后就不行。
可以断定问题出来user-servlet相关的程序里,于是还原注释重启并观察spring mvc日志,注意到有如下一段:
2012-04-11 18:25:36,446 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - Mapped URL path [/{userid}] onto handler 'UserController'
2012-04-11 18:25:36,446 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - Mapped URL path [/{userid}.*] onto handler 'UserController'
2012-04-11 18:25:36,446 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - Mapped URL path [/{userid}/] onto handler 'UserController'
6、分析:user-servlet处理*.do的链接,hessian-servlet处理/hessian/*的请求,而从spring mvc映射的url来看,/{userid}(Long类型参数)很可能去匹配了测试链接
7、尝试解决办法:让spring mvc只映射.do结尾的链接,修改@RequestMapping(value = "{userid}", method = RequestMethod.GET)
为@RequestMapping(value = "{userid}.do", method = RequestMethod.GET)
8、验证结果,spring mvc加载:[org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - Mapped URL path [/{userid}.do] onto handler 'UserController'
访问http://localhost/hessian/userHessianService
结果HTTP Status 405 - HessianServiceExporter only supports POST requests
得到预期结果,测试结束。
摘自 爱嘟嘟