3

This is working

@PostMapping(value="/foo", params={"add"})
public String add(@ModelAttribute Foo foo) {
    return "foo";
}

@PostMapping(value="/foo", params={"delete"})
public String delete(@ModelAttribute Foo foo) {
    return "foo";
}

Why is this one NOT working ?

@PostMapping("/foo")
public String add(@ModelAttribute Foo foo, @RequestParam String add) {
    return "foo";
}

@PostMapping("/foo")
public String delete(@ModelAttribute Foo foo, @RequestParam String delete) {
    return "foo";
}

With @PostMapping, I get following error message.

Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'webController' method

Why am I getting ErrorMessage when using @RequestParam ?

5
  • Yes. Just specify the same params attribute for each @PostMapping i.e. params={"...."}. The handler to be invoked will be resolved purely by its mapping and not by reference to any method args. Commented Jul 16, 2019 at 13:35
  • PostMapping is a simple RequestMapping which use POST as method. Commented Jul 16, 2019 at 13:38
  • 3
    It's because you can't have the same @PostMapping("/foo") mapping for two methods. Java method name does not play any role here. Please read documentation to learn how to map requests to controller properly: docs.spring.io/spring/docs/current/spring-framework-reference/… Commented Jul 16, 2019 at 13:50
  • you can change @PostMapping for delete method to @DeleteMapping Commented Jul 16, 2019 at 13:52
  • @philijack, does my solution help? Commented Jul 18, 2019 at 15:02

2 Answers 2

1

@RequestParam is resolved at two different levels on where you are using it.

As per spring documentation, parameters are

Supported at the type level as well as at the method level! When used at the type level, all method-level mappings inherit this parameter restriction (i.e. the type-level restriction gets checked before the handler method is even resolved).

In your first case the controller differentiates the two requests different as it is resolved at the type level itself. When you use the @RequestParam at the method level, you have the option of making the parameter as Optional by setting the required flag to false which makes it difficult to separate out your end points, hence the error.

Sign up to request clarification or add additional context in comments.

Comments

0

Try this one,

@PostMapping("/foo")
public String add(@ModelAttribute Foo foo, @RequestParam("add") String add) {
   return "foo";
}

@PostMapping("/foo")
public String delete(@ModelAttribute Foo foo, @RequestParam("delete") String delete){
    return "foo";
}

It will work. Because you did not specify the param and simply use the string to two functions. So the compiler decide both are same function then only it throws the ambiguous error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.