Consider this Spring MVC Controller:
@Controller
@RequestMapping("/test*")
public class TestController {
@RequestMapping(method = RequestMethod.GET)
public void doStuff(Model model){
...
}
@RequestMapping(params = "myParam")
public void doStuff(@RequestParam("myParam") int myParam, Model model){
...
}
}
When I put this into my browser:
mySite.com/test.html?myParam=1
I expected an AmbiguousHandlerMappingException or something, since both methods seem to match the URL.
But actually the first method got called. Does anybody know why?
The @RequestMapping annotation can be applied to class-level and/or method-level in a controller. The class-level annotation maps a specific request path or pattern onto a controller. You can then apply additional method-level annotations to make mappings more specific to handler methods.
A Callable can be returned when the application wants to produce the return value asynchronously in a thread managed by Spring MVC. A DeferredResult can be returned when the application wants to produce the return value from a thread of its own choosing.
Unfortunately, this is not possible. The request mapping has to be unique otherwise the application can't determine which method the incoming request should be mapped to.
@Controller annotation indicates that the class is a “controller” like a web controller. @RestController annotation indicates that class is a controller where @RequestMapping methods assume @ResponseBody semantics by default. In @Controller, we need to use @ResponseBody on every handler method.
This smells like a bug. If you add method=GET to the second handler, it works as expected, so that's the workaround.
I've filed a bug report on this, hopefully it'll be addressed.
http://jira.springframework.org/browse/SPR-5772
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With