試圖瞭解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/test
,AbstractHandlerMethodMapping
將第一查找任何處理程序方法爲/test
,如果發現(這在我的情況下,它確實)將請求傳遞給該方法 - TestController.test()
,在這種情況下。如果它找不到任何映射的處理程序方法,那麼它會查找/ user/test的任何處理程序方法。
我發現有點bizzare,但這是我在日誌中觀察到的。任何人都可以用一些官方文件來證明這一點嗎?
從正在發生的事情和記錄你的servlet被映射到'/ user'來判斷。該映射是爲'DispatcherServlet'內的url計算的。所以在這種情況下'/ test'。 – 2014-09-22 05:43:50