Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set priority on Spring MVC mapping?

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?

like image 622
JasonStoltz Avatar asked Jan 21 '13 14:01

JasonStoltz


People also ask

When to use@ order annotation?

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.

What is@ order in Spring?

@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).

Which element has to be used in order to Configure Spring MVC?

tag will be use to activate Spring MVC annotation scanning capability which allows to make use of annotations like @Controller and @RequestMapping etc.


1 Answers

I had very similar problem recently where I had to two kind of ambiguous URLs:

  • One was accepting a year, say /2012, /2013 etc. I created it using mapping with regular expression like this: @RequestMapping("/{year:(?:19|20)\\d{2}}")
  • Another one was accepting a content identifier (aka slug). I created it using mapping like @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.

like image 180
Tom Avatar answered Sep 20 '22 04:09

Tom