Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Narrowing problem with Spring MVC annotation-based controller and @RequestMapping

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?

like image 711
Daniel Alexiuc Avatar asked May 20 '09 01:05

Daniel Alexiuc


People also ask

What is true about @RequestMapping annotation in Spring MVC?

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.

Which of these can be the return type of a method annotated with @RequestMapping in Spring?

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.

What happens when two methods with different view has same request mapping?

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.

What are the uses of @RequestMapping and @RestController annotations in Spring boot?

@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.


1 Answers

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

like image 68
skaffman Avatar answered Sep 28 '22 18:09

skaffman