2014-09-21 202 views
2

试图了解Spring MVC中@RequestMapping的工作顺序。我有两个不同的控制器如下@requestmapping春季优先级顺序查询

@Controller 
public class TestController { 

    @RequestMapping(value="/test", method=RequestMethod.GET, produces="application/json") 
    public @ResponseBody RequestResponse test() { 
     log.info("test processed"); 
     .... 
    } 
} 

@Controller 
@RequestMapping("/user") 
public class UserController { 

    @RequestMapping(value="/test", method=RequestMethod.GET, produces="application/json") 
    public @ResponseBody RequestResponse userTest() { 
     log.info("user test processed"); 
     .... 
    } 
} 

当我提出一个要求www.test.com/user/test,我期待被称为UserController.userTest()方法,但我看到的是,的TestController。正在调用test()方法。

这里的日志记录开启春季

DEBUG; tid:http-bio-8080-exec-8; DispatcherServlet; DispatcherServlet with name 'dispatcher' processing GET request for [/test-web/user/test] 
DEBUG; tid:http-bio-8080-exec-8; AbstractHandlerMethodMapping; Looking up handler method for path /test 
DEBUG; tid:http-bio-8080-exec-8; AbstractHandlerMethodMapping; Returning handler method [public com.test.dto.RequestResponse com.test.controller.TestController.test()] 
DEBUG; tid:http-bio-8080-exec-8; AbstractBeanFactory; Returning cached instance of singleton bean 'testController' 

谁能类型和方法层面@RequestMapping订单或此的任何文件的顺序澄清?

调试了一些更多,并基于调试我可以推断,它似乎工作的方式(这绝不是对我的问题的答案)是在在Spring评估请求从右到左。

对于传入的请求/user/testAbstractHandlerMethodMapping将第一查找任何处理程序方法为/test,如果发现(这在我的情况下,它确实)将请求传递给该方法 - TestController.test(),在这种情况下。如果它找不到任何映射的处理程序方法,那么它会查找/ user/test的任何处理程序方法。

我发现有点bizzare,但这是我在日志中观察到的。任何人都可以用一些官方文件来证明这一点吗?

+0

从正在发生的事情和记录你的servlet被映射到'/ user'来判断。该映射是为'DispatcherServlet'内的url计算的。所以在这种情况下'/ test'。 – 2014-09-22 05:43:50

回答

0

首先检查级别RequestMapping,然后检查方法级别。所以你说的行为是不可能的。

+0

我已经添加了一些来自Spring的调试日志,我花了几个小时后发现这是发生的行为。我只是想了解它并纠正它。 – andthereitgoes 2014-09-21 21:19:31