引起:java.lang.IllegalStateException:找到了不明确的映射。无法映射“appController”bean 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33854225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:05:33  来源:igfitidea点击:

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

javaspringhibernate

提问by besmart

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 tomcat:

大家早上好,我正在处理一个我无法解码的模糊映射......我正在使用 Spring mvc 4.0.6 和 hibernate 4.3.6 我在 tomcat 中启动战争时遇到这个错误:

ERROR [localhost-startStop-2]: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration: Invocation of init method failed; nested exception is java.lang.IllegalStateException: 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.
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1553)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4727)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5167)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:945)
    at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1768)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.IllegalStateException: 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.
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.registerHandlerMethod(AbstractHandlerMethodMapping.java:192)
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.detectHandlerMethods(AbstractHandlerMethodMapping.java:164)
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.initHandlerMethods(AbstractHandlerMethodMapping.java:124)
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.afterPropertiesSet(AbstractHandlerMethodMapping.java:103)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.afterPropertiesSet(RequestMappingHandlerMapping.java:126)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549)
    ... 25 more

I can't understand why i'm getting this error. AppController is quite straight

我不明白为什么我会收到这个错误。AppController 很直接

package it.besmart.controller;
import it.besmart.models.Client;
import it.besmart.service.ClientService;
import java.util.List;
import java.util.Locale;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/")
public class AppController {

    @Autowired
    ClientService clientService;

    @Autowired
    MessageSource messageSource;

    @RequestMapping(value = { "/", "/list" }, method = RequestMethod.GET)
    public String listClients(ModelMap model){
        List<Client> clients = clientService.findAllClients();
        model.addAttribute("clients", clients);
        return "allclients";
    }

    @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";

    }


    @RequestMapping(value = { "/edit-{name}-client"}, method = RequestMethod.POST)
    public String updateClient(@Valid Client client, BindingResult result, ModelMap model, @PathVariable String name ){
        if(result.hasErrors()){
            return "registration";
        }

        if(!clientService.isClientNameUnique(client.getIdClient(), client.getNomeClient())){
            FieldError idErr = new FieldError("client", "name", messageSource.getMessage("non.unique.nome_client", new String[]{client.getNomeClient()}, Locale.getDefault()));
            result.addError(idErr);
            return "registration";
        }
        clientService.saveClient(client);
        model.addAttribute("success", "Client" + client.getNomeClient() + "aggiornato correttamente");

        return "success";

    }

    @RequestMapping(value = { "/delete-{id}-client" }, method = RequestMethod.GET)
        public String deleteClient(@PathVariable int id){
        clientService.deleteClientById(id);
        return "redirect:/list";
    }


    }

ClientService.java

客户端服务.java

package it.besmart.service;

import it.besmart.models.Client;

import java.util.List;

public interface ClientService {

    Client findById(int id);

    void saveClient(Client client);

    void updateClient(Client client);

    void deleteClientById(int id);

    List <Client> findAllClients();

    Client findClientByName(String name);

    boolean isClientNameUnique(Integer id, String name);

}

It looks to me like it's quite straight everything... I'm quite new to this kind of application.. Thanks

在我看来,一切都很简单......我对这种应用程序很陌生......谢谢

采纳答案by Luke Woodward

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.

发现不明确的映射。无法将 'appController' bean 方法 public java.lang.String it.besmart.controller.AppController.newClient(org.springframework.ui.ModelMap) 映射到 {[//new],methods=[POST],params=[], headers=[],consumes=[],produces=[],custom=[]}: 已经有'appController' bean 方法 public java.lang.String it.besmart.controller.AppController.saveClient(it.besmart.models .Client,org.springframework.validation.BindingResult,org.springframework.ui.ModelMap) 映射。

It's telling you you're mapping more than one method to handle a POSTto the URL /new. If the web browser makes a POSTrequest to the URL /new, which of your methods should handle it?

它告诉您您正在映射多个方法来处理 aPOST到 URL /new。如果 Web 浏览器POST向 URL发出请求/new,您的哪个方法应该处理它?

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.GETinstead of RequestMethod.POSTfor that.

我怀疑第一个是不正确的;您可能想要使用RequestMethod.GET而不是RequestMethod.POST为此。

回答by Shailesh Kumar Dayananda

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

它与此处报告的问题无关,但由于这是 google 中关于此问题的最热门搜索。我还想提到发生此问题的另一个原因是当您将控制器方法标记为私有时(这发生在我身上,因为我使用 IDE 自动完成方法)。

@RequestMapping(value="/products",  method=RequestMethod.POST)
private List<Product> getProducts() {
    return productService.getProducts();
}

Making it public should resolve the issue.

将其公开应该可以解决问题。

回答by Augustas

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.

当 TomcatTomcat 8.0\work\Catalina\localhost\没有被正确清除时也有同样的错误。不得不手动删除,重新启动Tomcat,然后应用程序运行没有错误。

回答by HA S

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";

}

回答by Philip Rego

In my case I couldn't find one of the methods in the error. The server wasn't being updated. Try clean and rebuild. If using intellij delete the [project dir]/target folder.

就我而言,我在错误中找不到其中一种方法。服务器没有更新。尝试清理并重建。如果使用 intellij,请删除 [project dir]/target 文件夹。

回答by VIKAS KESHARWANI

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

尝试概括这部分。也许相同的值会导致请求映射的歧义。