原因:java.lang.IllegalStateException:找到不明确的映射。无法映射';appController';bean方法
大家早上好,我正在处理一个我无法解码的模糊映射。。。 我正在使用SpringMVC4.0.6和Hibernate4.3.6 在tomcat中发动战争时,我遇到了以下错误:原因:java.lang.IllegalStateException:找到不明确的映射。无法映射';appController';bean方法,java,spring,hibernate,Java,Spring,Hibernate,大家早上好,我正在处理一个我无法解码的模糊映射。。。 我正在使用SpringMVC4.0.6和Hibernate4.3.6 在tomcat中发动战争时,我遇到了以下错误: ERROR [localhost-startStop-2]: Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingH
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";
}
}
package it.besmart.controller;
导入it.besmart.models.Client;
导入it.besmart.service.ClientService;
导入java.util.List;
导入java.util.Locale;
导入javax.validation.Valid;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.context.MessageSource;
导入org.springframework.stereotype.Controller;
导入org.springframework.ui.ModelMap;
导入org.springframework.validation.BindingResult;
导入org.springframework.validation.FieldError;
导入org.springframework.web.bind.annotation.PathVariable;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestMethod;
@控制器
@请求映射(“/”)
公共类AppController{
@自动连线
客户服务客户服务;
@自动连线
消息源消息源;
@RequestMapping(值={”/“,“/list”},方法=RequestMethod.GET)
公共字符串ListClient(ModelMap模型){
List clients=clientService.findAllClients();
model.addAttribute(“客户机”,客户机);
返回“所有客户端”;
}
@RequestMapping(值={“/new”},方法=RequestMethod.POST)
公共字符串newClient(ModelMap模型){
客户端=新客户端();
model.addAttribute(“客户机”,客户机);
model.addAttribute(“编辑”,false);
返回“注册”;
}
@RequestMapping(值={“/new”},方法=RequestMethod.POST)
公共字符串saveClient(@Valid Client-Client,BindingResult,ModelMap-model){
if(result.hasErrors()){
返回“注册”;
}
clientService.saveClient(客户端);
model.addAttribute(“success”,“Client”+Client.getNomeClient()+“registrato corretmante”);
返回“成功”;
}
@RequestMapping(值={/edit-{name}-client},方法=RequestMethod.POST)
公共字符串updateClient(@Valid Client Client,BindingResult,ModelMap model,@PathVariable String name){
if(result.hasErrors()){
返回“注册”;
}
如果(!clientService.isClientNameUnique(client.getIdClient(),client.getNomeClient())){
FieldError idErr=newfielderror(“客户端”,“名称”,messageSource.getMessage(“非唯一的.nome_客户端”,新字符串[]{client.getNomeClient()},Locale.getDefault()));
结果.加法器(idErr);
返回“注册”;
}
clientService.saveClient(客户端);
model.addAttribute(“success”,“Client”+Client.getNomeClient()+“aggiornato corretmente”);
返回“成功”;
}
@RequestMapping(值={“/delete-{id}-client”},方法=RequestMethod.GET)
公共字符串deleteClient(@PathVariable int-id){
clientService.deleteClientById(id);
返回“重定向:/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);
}
打包it.besmart.service;
导入it.besmart.models.Client;
导入java.util.List;
公共接口客户端服务{
客户端findById(int-id);
作废储蓄客户(客户);
无效更新客户端(客户端);
void deleteClientById(int id);
列出FindAllClient();
客户端findClientByName(字符串名称);
布尔值isClientNameUnique(整数id、字符串名称);
}
在我看来,一切都很简单。。。我不太熟悉这种应用程序。。
谢谢这是您收到的错误消息: 发现不明确的映射。无法映射“appController”bean方法 public java.lang.String it.besmart.controller.AppController.newClient(org.springframework.ui.ModelMap) 到{[//new],methods=[POST],params=[],headers=[],consumes=[],products=[],custom=[]}:已经映射了'appController'bean方法public java.lang.String it.besmart.controller.appController.saveClient(it.besmart.models.Client,org.springframework.validation.BindingResult,org.springframework.ui.ModelMap) 它告诉您,您正在映射多个方法来处理
POST
到URL/new
。如果web浏览器向URL发出POST
请求/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
。这与本文报道的问题无关,但因为这是谷歌在这个问题上最热门的搜索。我还想提到发生此问题的另一个原因是,当您将控制器方法标记为私有时(这种情况发生在我身上,因为我对方法使用IDE自动完成)
@RequestMapping(value=“/products”,method=RequestMethod.POST)
私有列表getProducts(){
return productService.getProducts();
}
公开应该可以解决这个问题 当Tomcat的
Tomcat 8.0\work\Catalina\localhost\
未正确清除时,也有相同的错误。必须手动删除,重新启动Tomcat,然后应用程序无错误运行。将参数添加到以下代码中,您就可以开始了
@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";
}
- 记住向url提供参数,例如或 根据url配置相应地更改控制器方法
@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)
尝试概括这一部分。可能是相同的值导致了请求映射的模糊性。我遇到了这个问题,并通过将
name
替换为value
I解决了这个问题
@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)
@PostMapping(name = "/greetings/sayHi")
public Object sayHi() {
return "Hello";
}
@PostMapping(value = "/greetings/sayHi")
public Object sayHi() {
return "Hello";
}