Reputation: 8905
I'm working on a Spring MVC project using Annotated Controller.
One thing that I'm interested in is about the order which @RequestMapping
instruction to be processed.
For example, I want all /green/basic/welcome
to be mapped to GreenController.welcome()
but green/{treeId}/{treeName}
to be mapped to GreenController.viewTree(treeId, treeName)
.
I guess I need to specify two @RequestMapping
with @RequestMapping
of /green/basic/welcome
to be processed first, so that it won't be interpreter as a call to GreenControllerviewTree("basic", "welcome")
.
Can you guys guide me on that?
Upvotes: 10
Views: 5893
Reputation: 6474
An exact match for a RequestMapping
will take precedence over one with a PathVariable
. So you would have two request mappings like you pointed out. One to handle the specific url, and the variable version will catch everything else. Spring checks for direct path matches before checking for path variable matches, so order does not matter unless you have two request mappings with the same number of path variables, which may spit out an IllegalStateException
Check the source of org.springframework.web.servlet.handler.AbstractHandlerMethodMapping
for the specifics. It is handled in lookupHandlerMethod()
.
To determine the best match of two RequestMapping
s that aren't exact matches, the compareTo() method of RequestMappingInfo
is used.
Upvotes: 10