简体   繁体   中英

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?

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.

URL mapping is determined by the order the mappings are discovered.

So you could force one controller to be created in spring context after the other controller.

This can be done using the depends-on="" attribute in the bean definition (in xml).

I am not sure if it uses the first mapping it finds, or the last.

Also, this is just theory, I have not actually tried this.

I see log messages like this:

17:29:01.618 [main] INF S o.s.w.s.h.SimpleUrlHandlerMapping - Mapped URL path [/resources/**] onto handler    'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0'
17:29:01.625 [main] INF S o.s.w.s.h.SimpleUrlHandlerMapping - Mapped URL path [/**] onto handler 'org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler#0'

So worth having a lok at SimpleUrlHandlerMapping to see how it works.

Automatic ordering on specificy does works as expected now. (Using spring 5.0.10)

In AntPatternComparator

        /**
         * Compare two patterns to determine which should match first, i.e. which
         * is the most specific regarding the current path.
         * @return a negative integer, zero, or a positive integer as pattern1 is
         * more specific, equally specific, or less specific than pattern2.
         */
        @Override
        public int compare(String pattern1, String pattern2)

Which is used from AbstractUrlHandlerMapping::lookupHandler(String urlPath, HttpServletRequest request)

        Comparator<String> patternComparator = getPathMatcher().getPatternComparator(urlPath);
        if (!matchingPatterns.isEmpty()) {
            matchingPatterns.sort(patternComparator);
            if (logger.isDebugEnabled()) {
                logger.debug("Matching patterns for request [" + urlPath + "] are " + matchingPatterns);
            }
            bestMatch = matchingPatterns.get(0);
        }

This url(/home.html) doesn't matches both mappings:

    @RequestMapping(value = "**/{content}.html")
    ...
    @RequestMapping(value = "**/home")
    …

It just match the first one because it has a suffix " .html " !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM