当前位置: 首页 > 面试题库 >

原因:java.lang.IllegalStateException:找到了模糊的映射。无法映射“ appController” Bean方法

朱渝
2023-03-14
问题内容

早上好,我正在处理一个模棱两可的映射,我无法解码…我正在使用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$1.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

我不明白为什么我会收到此错误。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

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

}

在我看来,这一切都是很直接的……对于这种应用程序,我还是一个新手。


问题答案:

这是您收到的错误消息:

找到模糊的映射。无法将“ appController”
bean方法公共java.lang.String映射为it.besmart.controller.AppController.newClient(org.springframework.ui.ModelMap)映射到{[//
new],方法= [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)映射。

它告诉您要映射多个方法来处理POSTURL /new。如果网络浏览器POST向URL 发出请求,那么/new您应该使用哪种方法处理该请求?

这是两种令人反感的方法:

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

    }

我怀疑其中第一个是不正确的。您可能想要使用RequestMethod.GET代替RequestMethod.POST



 类似资料:
  • 将x-www-form-urlencode数据发布到Spring。这是有效的: 另一方面,这给出了一个例外: 不支持内容类型'application/x-www-form-urlencoded;charset=utf-8' 这将导致bean上的所有字段为空: FUD: 表单数据: POM:

  • 我正在使用mapstruct从一个DTO映射到另一个DTO。我有多个默认方法,但其中2个返回值为String,并且使用相同的类作为输入参数,这给我带来了“使用java MapStruct的模糊映射方法”错误。 错误:(16,7)Java:在将属性“java.lang.String StatusHistory”映射到java.util.Map:java.util.Map toMap(java.lan

  • 问题内容: 我正在尝试构建一个应用程序,该应用程序可以列出一些来自数据库的值,并在必要时使用Spring 4进行修改,添加,删除,并且收到以下错误消息(仅当两个控制器文件中都存在“ @Controller”注释时,如果我从其中一个文件中删除了注释,那么它可以工作,但是我在控制台中收到一条消息“未找到映射……在名称为dispatcherservlet中…): 这是我的pom.xml文件 这是我的we

  • 我有一个带有两个endpoint的spring控制器,当访问它们时,它们会返回任意方法异常。我试图使用HeaderContentNegotiationStrategy来区分它们,它查看传入请求的Accept标头,以确定将请求映射到哪个方法。据我所知,这种策略应该将传入的accept头与products符号进行比较。在我的情况下,这两种方法都会生成一个应用程序/json媒体类型,但是Spring还允

  • 我有两种将实体映射到域的方法。 当我试图定义实体列表到域的映射方法时,我发现了用于映射集合元素的模糊映射方法。 有没有一种方法可以定义用于映射对象集合的方法

  • 我是修改Minecraft的初学者,希望修改最新版本的Minecraft锻造版(v1.17.1)。我知道如何为1.16.5设置gradle项目,并且在很大程度上可以为1.17.1设置gradle项目。问题是我不知道Forge 1.17.1的模糊映射是什么。我需要知道,以便为构建的第34行设置正确的映射。格拉德尔: