【问题标题】:Path Priority in RequestMappingRequestMapping 中的路径优先级
【发布时间】:2014-03-28 07:55:15
【问题描述】:

我已经为 here. 开了一个问题,但我也想问它给 stackoverflow 的人。

@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方法捕获这个请求的。

我试图深入研究 Spring 代码以了解那里发生了什么,然后我发现了 AntPatternComparator。该比较器给出结果以括号计数,取较小的一个以获得最佳匹配。

为什么?第三个比其他的更具体,有什么问题吗?

【问题讨论】:

    标签: java spring model-view-controller


    【解决方案1】:

    您可以手动将自己的RequestMappingHandlerMapping 版本添加到应用程序上下文中,并使用setPathMatcher(PathMatcher pathMatcher) 设置其patternMatcher 属性,并使用您自己的实现来解决您遇到的问题。

    【讨论】:

    • @Bart 感谢您的回复。我知道我可以配置它。但我想了解为什么春天会这样决定。我认为扩展它是危险的。
    • @oko 你说的危险是什么意思?你会扩展什么?您只需提供自己的PatMatcher
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-15
    • 1970-01-01
    • 2021-04-16
    • 2016-03-09
    • 1970-01-01
    • 2018-08-03
    • 1970-01-01
    相关资源
    最近更新 更多