java - Spring mvc Ambiguous mapping found. Cannot map controller bean method -
i trying build app can list values database , modify, add, delete if necessary using spring 4 , receive following error(only if "@controller" annotation present in both of controller files, if delete annotation 1 of files works message in console "no mapping found ... in dispatcherservlet name ...):
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 name 'org.springframework.web.servlet.mvc.method.annotation.requestmappinghandlermapping#0': invocation of init method failed; nested exception 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) {[/edit/{id}],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}: there 'bookcontroller' bean method public java.lang.string com.bookreview.app.bookcontroller.editbook(int,org.springframework.ui.model) mapped. @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.initializebean(abstractautowirecapablebeanfactory.java:1574) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:539) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:476) @ org.springframework.beans.factory.support.abstractbeanfactory$1.getobject(abstractbeanfactory.java:303) @ org.springframework.beans.factory.support.defaultsingletonbeanregistry.getsingleton(defaultsingletonbeanregistry.java:230) @ org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory.java:299) @ org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory.java:194) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.preinstantiatesingletons(defaultlistablebeanfactory.java:755) @ org.springframework.context.support.abstractapplicationcontext.finishbeanfactoryinitialization(abstractapplicationcontext.java:757) @ org.springframework.context.support.abstractapplicationcontext.refresh(abstractapplicationcontext.java:480) @ org.springframework.web.context.contextloader.configureandrefreshwebapplicationcontext(contextloader.java:403) @ org.springframework.web.context.contextloader.initwebapplicationcontext(contextloader.java:306) @ org.springframework.web.context.contextloaderlistener.contextinitialized(contextloaderlistener.java:106) @ org.apache.catalina.core.standardcontext.listenerstart(standardcontext.java:4728) @ org.apache.catalina.core.standardcontext.startinternal(standardcontext.java:5166) @ org.apache.catalina.util.lifecyclebase.start(lifecyclebase.java:150) @ org.apache.catalina.core.containerbase$startchild.call(containerbase.java:1409) @ org.apache.catalina.core.containerbase$startchild.call(containerbase.java:1399) @ java.util.concurrent.futuretask.run(futuretask.java:266) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1142) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:617) @ 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) {[/edit/{id}],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}: there 'bookcontroller' bean method public java.lang.string com.bookreview.app.bookcontroller.editbook(int,org.springframework.ui.model) mapped. @ org.springframework.web.servlet.handler.abstracthandlermethodmapping.registerhandlermethod(abstracthandlermethodmapping.java:212) @ org.springframework.web.servlet.handler.abstracthandlermethodmapping.detecthandlermethods(abstracthandlermethodmapping.java:184) @ org.springframework.web.servlet.handler.abstracthandlermethodmapping.inithandlermethods(abstracthandlermethodmapping.java:144) @ org.springframework.web.servlet.handler.abstracthandlermethodmapping.afterpropertiesset(abstracthandlermethodmapping.java:123) @ org.springframework.web.servlet.mvc.method.annotation.requestmappinghandlermapping.afterpropertiesset(requestmappinghandlermapping.java:126) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.invokeinitmethods(abstractautowirecapablebeanfactory.java:1633) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.initializebean(abstractautowirecapablebeanfactory.java:1570) ... 21 more
this pom.xml file
<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <groupid>com.bookreview.app</groupid> <artifactid>bookreviewapp</artifactid> <version>0.0.1-snapshot</version> <packaging>war</packaging> <name>bookreviewapp</name> <description>review app</description> <!-- <properties> generic properties <java.version>1.8</java.version> <project.build.sourceencoding>utf-8</project.build.sourceencoding> <project.reporting.outputencoding>utf-8</project.reporting.outputencoding> spring <spring-framework.version>4.0.3.release</spring-framework.version> hibernate / jpa <hibernate.version>4.3.5.final</hibernate.version> <hibernate.version>3.6.9.final</hibernate.version> logging <logback.version>1.0.13</logback.version> <slf4j.version>1.7.5</slf4j.version> </properties> --> <dependencies> <!-- spring --> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-context</artifactid> <version>4.1.6.release</version> <!-- exclude commons logging in favor of slf4j --> <exclusions> <exclusion> <groupid>commons-logging</groupid> <artifactid>commons-logging</artifactid> </exclusion> </exclusions> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-webmvc</artifactid> <version>4.1.6.release</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-tx</artifactid> <version>4.1.6.release</version> </dependency> <!-- hibernate --> <dependency> <groupid>org.hibernate</groupid> <artifactid>hibernate-core</artifactid> <version>4.3.9.final</version> </dependency> <dependency> <groupid>org.hibernate</groupid> <artifactid>hibernate-entitymanager</artifactid> <version>4.3.9.final</version> </dependency> <!-- apache commons dbcp --> <dependency> <groupid>commons-dbcp</groupid> <artifactid>commons-dbcp</artifactid> <version>1.4</version> </dependency> <!-- spring orm --> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-orm</artifactid> <version>4.1.6.release</version> </dependency> <!-- aspectj --> <dependency> <groupid>org.aspectj</groupid> <artifactid>aspectjrt</artifactid> <version>1.8.5</version> </dependency> <!-- logging --> <dependency> <groupid>org.slf4j</groupid> <artifactid>slf4j-api</artifactid> <version>1.7.12</version> </dependency> <dependency> <groupid>org.slf4j</groupid> <artifactid>jcl-over-slf4j</artifactid> <version>1.7.12</version> <scope>runtime</scope> </dependency> <dependency> <groupid>org.slf4j</groupid> <artifactid>slf4j-log4j12</artifactid> <version>1.7.12</version> <scope>runtime</scope> </dependency> <dependency> <groupid>log4j</groupid> <artifactid>log4j</artifactid> <version>1.2.15</version> <exclusions> <exclusion> <groupid>javax.mail</groupid> <artifactid>mail</artifactid> </exclusion> <exclusion> <groupid>javax.jms</groupid> <artifactid>jms</artifactid> </exclusion> <exclusion> <groupid>com.sun.jdmk</groupid> <artifactid>jmxtools</artifactid> </exclusion> <exclusion> <groupid>com.sun.jmx</groupid> <artifactid>jmxri</artifactid> </exclusion> </exclusions> <scope>runtime</scope> </dependency> <!-- @inject --> <dependency> <groupid>javax.inject</groupid> <artifactid>javax.inject</artifactid> <version>1</version> </dependency> <!-- servlet --> <dependency> <groupid>javax.servlet</groupid> <artifactid>servlet-api</artifactid> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupid>javax.servlet.jsp</groupid> <artifactid>jsp-api</artifactid> <version>2.2.1-b03</version> <scope>provided</scope> </dependency> <dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version>4.7</version> <scope>test</scope> </dependency> <dependency> <groupid>javax.servlet</groupid> <artifactid>jstl</artifactid> <version>1.2</version> </dependency> <!-- test --> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <version>5.1.35</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-aop</artifactid> <version>4.1.6.release</version> </dependency> </dependencies> </project>
this web.xml file
<?xml version="1.0" encoding="utf-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- definition of root spring container shared servlets , filters --> <context-param> <param-name>contextconfiglocation</param-name> <param-value>/web-inf/app/root-context.xml</param-value> </context-param> <!-- creates spring container shared servlets , filters --> <listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> <!-- processes application requests --> <servlet> <servlet-name>servlet</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-name>contextconfiglocation</param-name> <param-value>/web-inf/app/servlet/sevlet-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>servlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
this sevlet-context.xml file
<?xml version="1.0" encoding="utf-8"?> <beans:beans xmlns="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemalocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <!-- dispatcherservlet context: defines servlet's request-processing infrastructure --> <!-- enables spring mvc @controller programming model --> <annotation-driven /> <!-- handles http requests /resources/** efficiently serving static resources in ${webapproot}/resources directory --> <resources mapping="/resources/**" location="/resources/" /> <!-- resolves views selected rendering @controllers .jsp resources in /web-inf/views directory --> <beans:bean class="org.springframework.web.servlet.view.internalresourceviewresolver"> <beans:property name="prefix" value="/web-inf/views/" /> <beans:property name="suffix" value=".jsp" /> </beans:bean> <beans:bean id="datasource" class="org.apache.commons.dbcp.basicdatasource" destroy-method="close"> <beans:property name="driverclassname" value="com.mysql.jdbc.driver" /> <beans:property name="url" value="jdbc:mysql://localhost:3306/test" /> <beans:property name="username" value="serban" /> <beans:property name="password" value="serban" /> </beans:bean> <!-- hibernate 4 sessionfactory bean definition --> <beans:bean id="hibernate4annotatedsessionfactory" class="org.springframework.orm.hibernate4.localsessionfactorybean"> <beans:property name="datasource" ref="datasource" /> <beans:property name="annotatedclasses"> <beans:list> <beans:value>com.bookreview.app.model.book</beans:value> <beans:value>com.bookreview.app.model.review</beans:value> </beans:list> </beans:property> <beans:property name="hibernateproperties"> <beans:props> <beans:prop key="hibernate.dialect">org.hibernate.dialect.mysqldialect </beans:prop> <beans:prop key="hibernate.show_sql">true</beans:prop> </beans:props> </beans:property> </beans:bean> <beans:bean id="bookdao" class="com.bookreview.app.dao.bookdaoimpl"> <beans:property name="sessionfactory" ref="hibernate4annotatedsessionfactory" /> </beans:bean> <beans:bean id="bookservice" class="com.bookreview.app.service.bookserviceimpl"> <beans:property name="bookdao" ref="bookdao"></beans:property> </beans:bean> <beans:bean id="reviewdao" class="com.bookreview.app.dao.reviewdaoimpl"> <beans:property name="sessionfactory" ref="hibernate4annotatedsessionfactory" /> </beans:bean> <beans:bean id="reviewservice" class="com.bookreview.app.service.reviewserviceimpl"> <beans:property name="reviewdao" ref="reviewdao"></beans:property> </beans:bean> <!-- <default-servlet-handler/> --> <context:component-scan base-package="com.bookreview.app" /> <tx:annotation-driven transaction-manager="transactionmanager"/> <beans:bean id="transactionmanager" class="org.springframework.orm.hibernate4.hibernatetransactionmanager"> <beans:property name="sessionfactory" ref="hibernate4annotatedsessionfactory" /> </beans:bean> </beans:beans>
this bookcontroller.java file
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 , update book both @requestmapping(value= "/book/add", method = requestmethod.post) public string addbook(@modelattribute("book") book b){ if(b.getbookid() == 0){ //new book, add 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"; } }
and reviewcontroller.java file
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 , update review both @requestmapping(value= "/review/add", method = requestmethod.post) public string addreview(@modelattribute("review") review r){ if(r.getid() == 0){ //new review, add 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"; } }
thank
you should write
@controller("/review") public class reviewcontroller {
and
@controller("/book") public class bookcontroller {
because in code have 2 methods without explicit/unique path mapping(eg. if have call /edit/1 , impossible determine controller's method editbook bookcontroller
or reviewcontroller editreview
)
Comments
Post a Comment