Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correct Spring MVC RequestMapping order?

I have an existing Spring MVC service running with the following RequestMapping.

@RequestMapping(value = "{user}/**", method = RequestMethod.GET)

I now need to add a new method to the same controller with the following RequestMapping.

@RequestMapping(value = "api/{user}/**", method = RequestMethod.GET)

When I test against the new URL, Spring is always choosing the first function so that this second function is inaccessible.

Is there anyway that I can get Spring to use my new "api" method? The only option I can think of is to create a new Java servlet in my web.xml, but I'd like to do something simpler. I did try making two different controllers, with @Controller and @Controller("api"), but that did not solve the problem.

like image 507
David V Avatar asked Oct 02 '22 07:10

David V


1 Answers

Assuming that your parameter for user is String, Spring is assuming that you've been invoked for user api.

There are a couple of ways to solve this. The first (and best) is to make your request mappings more narrow: fully specify the mapping, rather than relying on "**" at the end.

The second approach is to disallow an "api" user, using a regex in your mapping. Something like the following should work:

@RequestMapping(value = "{user:[^a][^p][^i]}/**", method = RequestMethod.GET)
like image 126
kdgregory Avatar answered Oct 05 '22 08:10

kdgregory