Caused by: java.lang.IllegalStateException: Ambiguous mapping found. Cannot map 'appController' bean method

前端 未结 7 1219
感情败类
感情败类 2021-02-05 10:04

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

相关标签:
7条回答
  • 2021-02-05 10:16

    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.

    0 讨论(0)
  • 2021-02-05 10:23
    @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.

    0 讨论(0)
  • 2021-02-05 10:30

    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";
    
    }
    
    • Remember to supply params to your url eg https://localhost/url?filter=filterBy OR http://localhost/url/filter based on your url configuration change the controller method accordingly
    0 讨论(0)
  • 2021-02-05 10:36

    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.

    0 讨论(0)
  • 2021-02-05 10:36

    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";
    }
    
    0 讨论(0)
  • 2021-02-05 10:37

    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.

    0 讨论(0)
提交回复
热议问题