2014-03-28 67 views
2

我已經爲那個here.打開了一個問題但我也想問它到stackoverflow的人。RequestMapping中的路徑優先

@Controller 
@RequestMapping("/xxx") 
public class MyController { 

@RequestMapping("/**") 
public ModelAndView getPage() { 
    //some code 
} 

@RequestMapping("/**/yyy/") 
public ModelAndView getPageSecond() { 
    //some code 
} 

@RequestMapping("/**/yyy/{pathVariable}") 
public ModelAndView getPageThird(@PathVariable("pathVariable") Integer num) { 
    //some code 
} 

} 

假設我們有一個簡單的控制器那樣,和我送這些請求:

1)/xxx/aaa/bbb/yyy/ - >沒關係它將與getPageSecond方法進行映射,並會做他的工作。

2)/xxx/aaa/bbb/yyy/23 - >我認爲它必須映射到getPageThird方法,但奇怪的是,Spring通過getPage方法捕獲這個請求。

我試圖潛入春季代碼,瞭解發生了什麼事情,然後我發現AntPatternComparator。這個比較器給出了括號計數的結果,取最小值作爲最佳匹配。

爲什麼?第三個比其他人更具體,有什麼不對嗎?

回答

1

您可以手動將您自己的RequestMappingHandlerMapping版本添加到您的應用程序上下文中,並使用setPathMatcher(PathMatcher pathMatcher)patternMatcher屬性設置爲您自己的實施,以解決您遇到的問題。

+1

@感謝您的回覆。我知道我可以配置它。但我想明白爲什麼春天會這樣決定。我認爲擴展它是危險的。 – oko

+0

@oko你是什麼意思危險?你會延伸什麼?你只需要提供你自己的'PatMatcher'。 – Bart