Path Priority in RequestMapping

2019-07-16 07:05发布

问题:

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/ -->okay it will be mapped with getPageSecond method and will do his work.

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.

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 ?

回答1:

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.