找到Spring mvc Ambiguous mapping。 无法映射控制器bean方法

我正在尝试构建一个应用程序,它可以列出数据库中的一些值,并在必要时使用Spring 4进行修改,添加,删除,并且我收到以下错误(仅当我的两个控制器文件中都存在“@Controller”注释时,如果我从其中一个文件中删除注释,但我在控制台中收到一条消息“找不到映射…在带有名称的dispatcherservlet中…”:

INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/edit/{id}],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.bookReview.app.BookController.editBook(int,org.springframework.ui.Model) WARN : org.springframework.web.context.support.XmlWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0': Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping found. Cannot map 'reviewController' bean method public java.lang.String com.bookReview.app.ReviewController.editReview(int,org.springframework.ui.Model) to {[/edit/{id}],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}: There is already 'bookController' bean method public java.lang.String com.bookReview.app.BookController.editBook(int,org.springframework.ui.Model) mapped. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480) 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:4728) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5166) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1399) 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:745) Caused by: java.lang.IllegalStateException: Ambiguous mapping found. Cannot map 'reviewController' bean method public java.lang.String com.bookReview.app.ReviewController.editReview(int,org.springframework.ui.Model) to {[/edit/{id}],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}: There is already 'bookController' bean method public java.lang.String com.bookReview.app.BookController.editBook(int,org.springframework.ui.Model) mapped. at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.registerHandlerMethod(AbstractHandlerMethodMapping.java:212) at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.detectHandlerMethods(AbstractHandlerMethodMapping.java:184) at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.initHandlerMethods(AbstractHandlerMethodMapping.java:144) at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.afterPropertiesSet(AbstractHandlerMethodMapping.java:123) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.afterPropertiesSet(RequestMappingHandlerMapping.java:126) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570) ... 21 more 

这是我的pom.xml文件

  4.0.0 com.bookReview.app BookReviewApp 0.0.1-SNAPSHOT war BookReviewApp review app <!--  Generic properties 1.8 UTF-8 UTF-8 Spring 4.0.3.RELEASE Hibernate / JPA 4.3.5.Final 3.6.9.Final Logging 1.0.13 1.7.5  -->    org.springframework spring-context 4.1.6.RELEASE    commons-logging commons-logging     org.springframework spring-webmvc 4.1.6.RELEASE   org.springframework spring-tx 4.1.6.RELEASE    org.hibernate hibernate-core 4.3.9.Final   org.hibernate hibernate-entitymanager 4.3.9.Final    commons-dbcp commons-dbcp 1.4    org.springframework spring-orm 4.1.6.RELEASE    org.aspectj aspectjrt 1.8.5    org.slf4j slf4j-api 1.7.12   org.slf4j jcl-over-slf4j 1.7.12 runtime   org.slf4j slf4j-log4j12 1.7.12 runtime   log4j log4j 1.2.15   javax.mail mail   javax.jms jms   com.sun.jdmk jmxtools   com.sun.jmx jmxri   runtime    javax.inject javax.inject 1    javax.servlet servlet-api 2.5 provided   javax.servlet.jsp jsp-api 2.2.1-b03 provided   junit junit 4.7 test   javax.servlet jstl 1.2    mysql mysql-connector-java 5.1.35   org.springframework spring-aop 4.1.6.RELEASE    

这是我的web.xml文件

     contextConfigLocation /WEB-INF/app/root-context.xml    org.springframework.web.context.ContextLoaderListener    servlet org.springframework.web.servlet.DispatcherServlet  contextConfigLocation /WEB-INF/app/servlet/sevlet-context.xml  1   servlet /   

这是我的sevlet-context.xml文件

                        com.bookReview.app.model.book com.bookReview.app.model.review     org.hibernate.dialect.MySQLDialect  true                <!--  -->       

这是我的BookController.java文件

 package com.bookReview.app; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import com.bookReview.app.model.book; import com.bookReview.app.service.BookService; @Controller public class BookController { private BookService bookService; @Autowired(required=true) @Qualifier(value="bookService") public void setBookService(BookService bs){ this.bookService = bs; } @RequestMapping(value = "/books", method = RequestMethod.GET) public String listBooks(Model model) { model.addAttribute("book", new book()); model.addAttribute("listBooks", this.bookService.listBooks()); return "book"; } //For add and update book both @RequestMapping(value= "/book/add", method = RequestMethod.POST) public String addBook(@ModelAttribute("book") book b){ if(b.getBookid() == 0){ //new book, add it this.bookService.addBook(b); }else{ //existing book, call update this.bookService.updateBook(b); } return "redirect:/books"; } @RequestMapping("/remove/{id}") public String removeBook(@PathVariable("id") int id){ this.bookService.removeBook(id); return "redirect:/books"; } @RequestMapping("/edit/{id}") public String editBook(@PathVariable("id") int id, Model model){ model.addAttribute("book", this.bookService.getBookById(id)); model.addAttribute("listBooks", this.bookService.listBooks()); return "book"; } } 

这是我的ReviewController.java文件

 package com.bookReview.app; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import com.bookReview.app.model.review; import com.bookReview.app.service.ReviewService; @Controller public class ReviewController { private ReviewService reviewService; @Autowired(required=true) @Qualifier(value="reviewService") public void setReviewService(ReviewService rs){ this.reviewService = rs; } @RequestMapping(value = "/reviews", method = RequestMethod.GET) public String listReviews(Model model) { model.addAttribute("book", new review()); model.addAttribute("listReviews", this.reviewService.listReviews()); return "review"; } //For add and update review both @RequestMapping(value= "/review/add", method = RequestMethod.POST) public String addReview(@ModelAttribute("review") review r){ if(r.getId() == 0){ //new review, add it this.reviewService.addReview(r); }else{ //existing review, call update this.reviewService.updateReview(r); } return "redirect:/reviews"; } @RequestMapping("/remove/{id}") public String removeReview(@PathVariable("id") int id){ this.reviewService.removeReview(id); return "redirect:/reviews"; } @RequestMapping("/edit/{id}") public String editReview(@PathVariable("id") int id, Model model){ model.addAttribute("review", this.reviewService.getReviewById(id)); model.addAttribute("listReviews", this.reviewService.listReviews()); return "review"; } } 

谢谢

你应该写

 @Controller("/review") public class ReviewController { 

 @Controller("/book") public class BookController { 

因为在你的代码中你有两种方法没有用于映射的显式/唯一路径(例如,如果我们有一个call / edit / 1,那么从editBook BookControllerReviewController editReview确定一个控制器的方法是不可能的)

如果问题是关于ambigious方法,可能@RequestMapping应该是问题。 从@RequestMapping( name = …)更改为@RequestMapping( value = …)

 @RequestMapping(name = "xxx.htm", method = RequestMethod.GET) 

 @RequestMapping(value = "xxx.htm", method = RequestMethod.GET)