[英]Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'com.sportswin.soa.spark.controller.ISparkGameUserController' method
[英]Caused by: java.lang.IllegalStateException: Ambiguous mapping found. Cannot map 'appController' bean method
大家早上好,我正在處理一個我無法解碼的模糊映射......我正在使用 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";
}
}
客戶端服務程序
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 方法 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) 映射。
它告訴您您正在映射多個方法來處理POST
到 URL /new
。 如果 Web 瀏覽器向 URL /new
發出POST
請求,您的哪個方法應該處理它?
以下是兩種違規方法:
@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
。
就我而言,我在錯誤中找不到其中一種方法。 服務器沒有更新。 嘗試清理並重建。 如果使用 intellij,請刪除 [project dir]/target 文件夾。
當 Tomcat 的Tomcat 8.0\\work\\Catalina\\localhost\\
沒有被正確清除時也有同樣的錯誤。 不得不手動刪除,重新啟動Tomcat,然后應用程序運行沒有錯誤。
它與此處報告的問題無關,但由於這是 google 中關於此問題的最熱門搜索。 我還想提到發生此問題的另一個原因是當您將控制器方法標記為私有時(這發生在我身上,因為我使用 IDE 自動完成方法)。
@RequestMapping(value="/products", method=RequestMethod.POST)
private List<Product> getProducts() {
return productService.getProducts();
}
將其公開應該可以解決問題。
將參數添加到以下代碼中,您就可以開始了。
@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";
}
@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
value
替換name
來解決它。 請求處理程序原為
@PostMapping(name = "/greetings/sayHi")
public Object sayHi() {
return "Hello";
}
替換鍵name
@PostMapping(value = "/greetings/sayHi")
public Object sayHi() {
return "Hello";
}
在我的情況下,上層之一已經定義了映射請求。 這就是為什么它給我錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.