RequestMapping中的路径优先级 [英] Path Priority in RequestMapping
问题描述
我已经在这里打开了一个问题.但我也想问stackoverflow 的人.
I already opened an issue for that here. But also i want to ask it to stackoverflow people.
@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
}
}
假设我们有一个像这样的简单控制器,我正在发送这些请求:
Assume that we have a simple Controller like that, and I am sending these requests :
1) /xxx/aaa/bbb/yyy/
-->好吧,它会用 getPageSecond
方法映射并完成他的工作.
1) /xxx/aaa/bbb/yyy/
-->okay it will be mapped with getPageSecond
method and will do his work.
2) /xxx/aaa/bbb/yyy/23
--> 我觉得肯定是用getPageThird
方法映射的,但是奇怪的是Spring在抓此请求通过 getPage
方法.
2) /xxx/aaa/bbb/yyy/23
--> I think it must be mapped with getPageThird
method, but it is strange that Spring is catching this request via getPage
method.
我试图深入研究 Spring 代码以了解其中发生了什么,然后我发现了 AntPatternComparator.此比较器给出结果以括号计数,取较小的一个以获得最佳匹配.
I tried to dive into Spring codes to understand whats going on there, then i found AntPatternComparator. This comparator is giving result in order to bracket count, taking the lesser one for best match.
为什么?第三个比其他的更具体,有什么问题吗?
Why? Third one is more specific then others, is there something wrong ?
推荐答案
您可以手动将您自己版本的 RequestMappingHandlerMapping
添加到您的应用程序上下文中,并使用以下方法设置其 patternMatcher
属性setPathMatcher(PathMatcher pathMatcher)
使用您自己的实现来解决您遇到的问题.
You could manually add your own version of RequestMappingHandlerMapping
to your application context and set its patternMatcher
property using setPathMatcher(PathMatcher pathMatcher)
with your own implementation that will correct the issue you're having.
这篇关于RequestMapping中的路径优先级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!