I have 2 spring controller mappings:
@Controller
public class ContentController {
@RequestMapping(value = "**/{content}.html")
public String content(@PathVariable String content, Model model, HttpServletRequest request) {
}
}
@Controller
public class HomeController {
@RequestMapping(value = "**/home")
public String home(HttpServletRequest request, Model model) {
}
}
The following url matches both mappings: /home.html
However, I want to ensure that the 'content' mapping ALWAYS has priority over the 'home' mapping. Is there a way I can specify that?
When to Use @Order. Before Spring 4.0, the @Order annotation was used only for the AspectJ execution order. It means the highest order advice will run first. Since Spring 4.0, it supports the ordering of injected components to a collection.
@Order defines the sort order for an annotated component. The value() is optional and represents an order value as defined in the Ordered interface. Lower values have higher priority. The default value is Ordered. LOWEST_PRECEDENCE , indicating the lowest priority (losing to any other specified order value).
tag will be use to activate Spring MVC annotation scanning capability which allows to make use of annotations like @Controller and @RequestMapping etc.
I had very similar problem recently where I had to two kind of ambiguous URLs:
/2012
, /2013
etc. I created it using mapping with regular expression like this: @RequestMapping("/{year:(?:19|20)\\d{2}}")
@RequestMapping("/{slug}")
.It was important that the "year" controller method was taking precedence over the "slug" one. Unfortunately (for me) Spring was always using the "slug" controller method.
As Spring MVC prefers more specific mappings, I had to make my "slug" pattern less specific. Based on Path Pattern Comparison documentation, I added wild card to the slug mapping: @RequestMapping("/{slug}**")
My controllers look like that and now listByYear
is called when a year (/2012
, /1998
etc) is in 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";
}
}
This is not exactly how to set up a priority (which in my opinion would be an amazing feature) but give some sort of "nice" workaround and might be handy in the future.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With