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 <servlet>
2 <servlet-name>user-servlet</servlet-name>
3 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
4 <load-on-startup>1</load-on-startup>
5 </servlet>
6 <servlet-mapping>
7 <servlet-name>user-servlet</servlet-name>
8 <url-pattern>*.do</url-pattern>
9 </servlet-mapping>
10 <servlet>
11 <servlet-name>hessian-servlet</servlet-name>
12 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
13 <init-param>
14 <param-name>contextConfigLocation</param-name>
15 <param-value>
16 classpath:spring/hessian-remoting.xml
17 </param-value>
18 </init-param>
19 <load-on-startup>2</load-on-startup>
20 </servlet>
21 <servlet-mapping>
22 <servlet-name>hessian-servlet</servlet-name>
23 <url-pattern>/hessian/*</url-pattern>
24 </servlet-mapping>
3、hessian-remoting.xml中服务配置:
1 <bean name="/userHessianService" class="org.springframework.remoting.caucho.HessianServiceExporter">
2 <property name="service" ref="userService"/>
3 <property name="serviceInterface" value="com.cd.common.UserService"/>
4 </bean>
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
得到预期结果,测试结束。