早上好,我正在处理一个不能解码的模糊映射...... 我使用的是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);
}
在我看来,它非常直接......我对这种应用很陌生。 感谢
答案 0 :(得分:12)
这是您收到的错误消息:
找到了模糊的映射。无法映射'appController'bean方法 public java.lang.String it.besmart.controller.AppController.newClient(org.springframework.ui.ModelMap) to {[// new],methods = [POST],params = [],headers = [],consume = [],produce = [],custom = []}:已经有'appController'bean方法
它告诉您,您正在映射多个方法来处理POST
到网址/new
。如果网络浏览器向网址POST
发出/new
请求,您的哪个方法应该处理它?</ p>
以下是两种违规方法:
@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
。
答案 1 :(得分:0)
这与此处报道的问题无关,但由于这是谷歌在此问题上搜索量最大的问题。我还想提一下这个问题发生的另一个原因是当你将控制器方法标记为私有时(它发生在我身上,因为我使用IDE自动完成方法)。
@RequestMapping(value="/products", method=RequestMethod.POST)
private List<Product> getProducts() {
return productService.getProducts();
}
将其公之于众应该解决问题。
答案 2 :(得分:0)
当Tomcat的Tomcat 8.0\work\Catalina\localhost\
未正确清除时,也会出现相同的错误。不得不手动删除,重启Tomcat,然后app运行没有错误。
答案 3 :(得分:0)
将参数添加到以下代码中,您就很好了。
@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";
}
答案 4 :(得分:0)
就我而言,我找不到错误中的一种方法。服务器未更新。尝试清洁并重建。如果使用intellij,请删除[项目目录] /目标文件夹。
答案 5 :(得分:0)
df <- structure(list(month = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L,
3L, 3L, 4L, 4L, 4L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L,
4L), .Label = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
"Aug", "Sep", "Oct", "Nov", "Dec"), class = c("ordered", "factor"
)), site = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L,
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), .Label = c("port",
"bluff", "palm"), class = "factor"), o2 = c(-0.230806451612904,
-0.235637733188544, -0.245057174556773, -0.164558508989615, -0.213785860472091,
-0.0743139564452465, -0.148132987820631, -0.18420332235214, -0.506846587196195,
-0.195636532933123, -0.19852591713126, -0.609512525218665, 0.00176680974320632,
0.00148126891095111, 0.000339290425939857, 0.00286401267875333,
0.00351454109668776, 0.0017904651506876, 0.00124172906552544,
0.00296532826689073, 0.00147283622695563, 0.000615469748914681,
0.00288144920417381, 0.000610425917237045), sd = c(0.13092880400035,
0.0293651765468604, 0.186903186503979, 0.0165030189596949, 0.035275044506981,
0.0117338426583851, 0.124600245537606, 0.0329422384479664, 0.321892933123791,
0.129212671278402, 0.0629054816199097, 0.157196511439801, 8.01053634208091e-05,
0.000676914258685707, 0.00042433026354163, 0.000120510874443334,
0.000729790000379787, 0.000750152053448522, 0.000883147983451001,
0.000397030811318241, 0.00107944390293209, 0.000315781661923161,
0.00218786030744793, 0.000184295714801481), n = c(2L, 2L, 2L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L), se = c(0.0925806451612918, 0.0207643154670252,
0.132160510602338, 0.00952802243882137, 0.0203660564417815, 0.00677453721744733,
0.0719379853022306, 0.0190192102356423, 0.185844971589259, 0.0746009705452959,
0.0363184967467579, 0.0907574481954395, 5.66430456842669e-05,
0.000478650662598528, 0.000300046806812961, 6.9576985800136e-05,
0.000421344453171167, 0.000433100489991655, 0.00050988572597971,
0.000229225845791162, 0.000623217227932945, 0.000182316627516485,
0.00126316173745436, 0.000106403180551129), trmt = c("res", "res",
"res", "res", "res", "res", "res", "res", "res", "res", "res",
"res", "pro", "pro", "pro", "pro", "pro", "pro", "pro", "pro",
"pro", "pro", "pro", "pro")), row.names = c(NA, -24L), class = "data.frame")
library(ggplot2)
plot <- ggplot(df, aes(x=month, y=o2, fill=site))+
geom_bar(stat="identity", color="black", width=0.4, position=position_dodge()) +
geom_errorbar(data=subset(df, df$trmt=="res"), aes(ymin=o2-se, ymax=o2), width=0.2, position=position_dodge(0.4)) +
scale_x_discrete("Month")+ #chronological order for plotting
scale_y_continuous(breaks =seq(-0.8, 0, by = 0.1), expand = c(0,0), limits=c(-0.8, 0))+
ylab(yaxis) +
scale_fill_manual(name = "Site",
labels = c("Port", "Bluff", "Palm"),
values = c("#FC4E07","#00AFBB", "#C3D7A4"))+
theme(panel.border = element_blank()) +
theme_bw() +
theme(axis.title.x = element_text(size=18, margin = margin(t=20, r=0, b=0, l=0)),
axis.title.y = element_text(size=18, margin = margin(t=0, r=20, b=0, l=0)),
axis.text.x = element_text(size=16, color="black"), #hjust <- l'éspace des axes
axis.text.y = element_text(size=16, color="black"),
legend.title = element_text(size=15),
legend.text = element_text(size=13),
plot.margin = unit(c(1,1,1,1), "cm")) +
theme(axis.line = element_line(colour = "black"), #remove top and right borders
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank()) +
geom_hline(yintercept = 0)
尝试概括这一部分。可能值相同会导致请求映射的歧义。
答案 6 :(得分:0)
我遇到了此问题,并通过在name
中将value
替换为PostMapping
来解决。请求处理程序为
@PostMapping(name = "/greetings/sayHi")
public Object sayHi() {
return "Hello";
}
替换密钥name
@PostMapping(value = "/greetings/sayHi")
public Object sayHi() {
return "Hello";
}