Reputation: 50318
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)
Upvotes: 6
Views: 5769
Reputation: 166
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.
Upvotes: 4
Reputation: 354
This not seems to be a problem, this is a valid mapping. If you have a look to http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping-uri-templates
In the section 16.3.2 Mapping Requests With @RequestMapping exists two methods doing exactly the same that you are trying.
To be sure that your classes are being compiled try to add a @RequestMapping("/someprefix") at class level to see if the URL is being exposed as you want.
I verify your example locally using the version 3.1.0.RELEASE and no issue were present.
As a workaround (and also to provide a well-understand REST URI add some context to your second mapping:
@RequestMapping(value={"client/support"}) // i.e: if you are working with clients
public ModelAndView getsupport(@PathVariable Long supportId){
// do your code here something here
}
Of course that this is valid if this is the unique controller present in the system, otherwise you must use RequestMapping at class level as I suggested above.
I hope this helps.
Upvotes: 1
Reputation: 49935
I am not seeing this behavior with Spring 3.1.2, it could potentially have been a bug with an older Spring version. Here is a gist which runs through without any issues for me - https://gist.github.com/3802254
Upvotes: 0
Reputation: 115368
If 2 these methods are defined in 2 different controllers your problem is that you have 2 controllers mapped to same URL. You do not control the order of controllers initialization right now, so the order is random.
I think you need /support
mapping to be initialized before /{section}
.
To achieve this try to define that controller "section" depends on controller "support". If this will not help try to put both methods together to one controller and put method mapped to "support" before "section"
I this does not work here is other suggestion. What "section" is? If it can accept limited number of values it should be defined as enum
. I believe that in this case everything will work as required if support and section methods are in one controller or in separate controllers.
Good luck.
Upvotes: 1