Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define RequestMapping prioritization

I have a situation where I need the following RequestMapping:

@RequestMapping(value={"/{section}"})
...method implementation here...

@RequestMapping(value={"/support"})
...method implementation here...

There is an obvious conflict. My hope was that Spring would resolve this automatically and map /support to the second method, and everything else to the first, but it instead maps /support to the first method.

How can I tell Spring to allow an explicit RequestMapping to override a RequestMapping with a PathVariable in the same place?

Edit 2: It seems that it would work if the /support mapping came before the /{section} mapping. Unfortunately we have dozens of controllers containing numerous methods with RequestMapping. How can I make sure that the controller with the /{section} mapping is initialized last? Or would a pre-interceptor be the way to go?

Edit 1: This is simplified, I know that having those two RequestMapping alone wouldn't make much sense)

like image 436
James Skidmore Avatar asked Sep 28 '12 21:09

James Skidmore


Video Answer


1 Answers

Using Spring you can extend the org.springframework.web.HttpRequestHandler to support your scenario.

Implement the method:

@Override
public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}

Use it to analyze the incoming request, determine if the request url is part of your special subset of request url's and forward to the appropriate location.

Ex:

@Override
public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
/** You will want to check your array of values and have this data cached  **/
if (urlPath.contains("/sectionName")) {
        RequestDispatcher requestDispatcher = request.getRequestDispatcher("sections" + "/" + urlPath);
        requestDispatcher.forward(request, response);
    }

}

And setup your sections such as:

@RequestMapping(value={"/sections/{sectionName}"})

This will not interfere with any of your pre-existing controller mappings.

like image 59
dwtalk Avatar answered Oct 01 '22 01:10

dwtalk