I had a very similar problem lately when I had two types of ambiguous URLs:
- One took a year, say
/2012 , /2013 , etc. I created it using regular expression matching as follows: @RequestMapping("/{year:(?:19|20)\\d{2}}") - The other accepted a content identifier (aka slug). I created it using matching, for example
@RequestMapping("/{slug}") .
It was important that the controller method "year" take precedence over "slug". Unfortunately (for me), Spring always used the slug controller method.
As Spring MVC prefers more specific mappings, I had to make my slug template less specific. Based on the Comparison of Path Patterns , I added a wild card to the bullet display: @RequestMapping("/{slug}**")
My controllers look like this, and now listByYear is called when the year ( /2012 , /1998 , etc.) is in the url.
@Controller public class ContentController { @RequestMapping(value = "/{slug}**") public String content(@PathVariable("slug") final String slug) { return "content"; } }
and
@Controller public class IndexController { @RequestMapping("/{year:(?:19|20)\\d{2}}") public String listByYear() { return "list"; } }
It’s not entirely accurate how to set the priority (which, in my opinion, would be an amazing feature), but give some kind of “pleasant” workaround and may be convenient in the future.
source share