Good morning all, i\'m dealing with an Ambiguous mapping i cannot decode... I\'m using Spring mvc 4.0.6 and hibernate 4.3.6 I\'m getting this error while launching the war in to
This is the error message you are getting:
Ambiguous mapping found. Cannot map 'appController' bean method public java.lang.String it.besmart.controller.AppController.newClient(org.springframework.ui.ModelMap) to {[//new],methods=[POST],params=[],headers=[],consumes=[],produces=[],custom=[]}: There is already 'appController' bean method public java.lang.String it.besmart.controller.AppController.saveClient(it.besmart.models.Client,org.springframework.validation.BindingResult,org.springframework.ui.ModelMap) mapped.
It's telling you you're mapping more than one method to handle a POST
to the URL /new
. If the web browser makes a POST
request to the URL /new
, which of your methods should handle it?
Here are the two offending methods:
@RequestMapping(value = {"/new"}, method = RequestMethod.POST)
public String newClient(ModelMap model){
Client client = new Client();
model.addAttribute("client", client);
model.addAttribute("edit", false);
return "registration";
}
@RequestMapping(value = {"/new"}, method = RequestMethod.POST)
public String saveClient(@Valid Client client, BindingResult result, ModelMap model){
if(result.hasErrors()){
return "registration";
}
clientService.saveClient(client);
model.addAttribute("success", "Client" + client.getNomeClient() + "registrato correttamente");
return "success";
}
I suspect that the first of these is incorrect; you probably want to use RequestMethod.GET
instead of RequestMethod.POST
for that.
@RequestMapping(value = {"/new"}, method = RequestMethod.POST)
public String newClient(ModelMap model)
@RequestMapping(value = {"/new"}, method = RequestMethod.POST)
public String saveClient(@Valid Client client, BindingResult result, ModelMap)
Try generalizing this part. Maybe values being the same are causing ambiguity for request mapping.
Add params to the following code and you are good to go.
@RequestMapping(value = {"/new"}, method = RequestMethod.POST, params = "filter")
public String saveClient(@PathVariable("filter") final String filter,@Valid Client client, BindingResult result, ModelMap model){
if(result.hasErrors()){
return "registration";
}
clientService.saveClient(client);
model.addAttribute("success", "Client" + client.getNomeClient() + "registrato correttamente");
return "success";
}
Also had same error when Tomcat's Tomcat 8.0\work\Catalina\localhost\
not being cleared properly. Had to delete by hand, restart Tomcat, then app run with no errors.
I encountered this and solved it by replacing name
with value
in PostMapping
. Request Handler was
@PostMapping(name = "/greetings/sayHi")
public Object sayHi() {
return "Hello";
}
Replacing key name
@PostMapping(value = "/greetings/sayHi")
public Object sayHi() {
return "Hello";
}
It is unrelated to the issue reported here, butsince this is the top most search in google on this issue. I wanted to also mention another reason this issue occurs is when you mark your controller method as private (It happens to me because I use IDE auto-complete for methods).
@RequestMapping(value="/products", method=RequestMethod.POST)
private List<Product> getProducts() {
return productService.getProducts();
}
Making it public should resolve the issue.