How to prioritize a request

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. I hope Spring will resolve this automatically and map /support to the second method, and everything else to the first, but instead map /support to the first method.

How can I tell Spring so that explicit RequestMapping can override RequestMapping with PathVariable in the same place?

Edit 2: This seems to work if the /support display appeared before the /{section} display. Unfortunately, we have dozens of controllers containing many methods with RequestMapping . How can I make sure that the controller with the display of /{section} initialized last? Or could an interceptor pass?

Edit 1: This is simplified, I know that having only those two RequestMapping does not make sense)

+6
source share
4 answers

Using Spring, you can extend org.springframework.web.HttpRequestHandler to support your script.

Implement the method:

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

Use it to analyze an incoming request, determine if the request URL is part of your special subset of the request URL and redirected to the appropriate location.

Example:

 @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 customize your sections, for example:

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

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

+3
source

If these two methods are defined in two different controllers, your problem is that you have two controllers mapped to the same URL. You do not control the initialization order of the controllers right now, so the order is random.

I think you need /support matching to initialize to /{section} .

To achieve this, try to determine that the "controller" section is dependent on controller support. If this does not help bring both methods together to the same controller and place the method associated with the "support" before the "section"

I do not work here, this is another offer. What "section" is there? If it can take a limited number of values, it should be defined as enum . I believe that in this case everything will work as needed if the support and partition methods are in the same controller or in separate controllers.

Good luck.

+1
source

This does not seem to be a problem, it is a valid mapping. If you look at http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping-uri-templates

Section 16.3.2. Matching requests with @RequestMapping there are two methods that do the same thing you are trying.

To make sure your classes compile, try adding @RequestMapping ("/ someprefix") at the class level to see if the URL is displayed as you like.

I check your example locally using version 3.1.0.RELEASE, and there were no problems.

As a workaround (and also to provide a well-understood REST URI, add some context to your second display:

 @RequestMapping(value={"client/support"}) // ie: if you are working with clients public ModelAndView getsupport(@PathVariable Long supportId){ // do your code here something here } 

Of course, this is true if it is a unique controller present in the system, otherwise you should use RequestMapping at the class level, as I suggested above.

Hope this helps.

+1
source

I do not see this behavior with Spring 3.1.2, this could potentially be a bug with the old version of Spring. Here is the gist that passes without any problems for me - https://gist.github.com/3802254

0
source

Source: https://habr.com/ru/post/926555/


All Articles