java - Spring REST Service with IOC? -
in past did simple soap services using spring , ioc (application-context.xml state controller has 2 different daos etc. , spring sets them me)
now trying rest service using spring.
my controller looks this
@restcontroller @requestmapping("/") public class mycontroller { private mydao1 mydao1; private mydao2 mydao2; @requestmapping("/name") public mytest getgreeting() { mytest tst = new mytest(1, "hallo "); return tst; } public void setmydao1(mydao1 mydao1) { this.mydao1 = mydao1; } public void setmydao2(mydao2 mydao2) { this.mydao2 = mydao2; }
my rest-servlet.xml contains this:
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemalocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- configure plugin json request , response in method handler --> <bean class="org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter"> <property name="messageconverters"> <list> <ref bean="jsonmessageconverter" /> </list> </property> </bean> <!-- configure bean convert json pojo , vice versa --> <bean id="jsonmessageconverter" class="org.springframework.http.converter.json.mappingjackson2httpmessageconverter"> </bean> <bean id="txmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager"> <property name="datasource" ref="datasource" /> </bean> <bean id="datasource" class="org.springframework.jndi.jndiobjectfactorybean"> <property name="jndiname" value="java:comp/env/jdbc/informixdb" /> <property name="resourceref" value="true" /> </bean> <bean id="mydao1" class="com.test.dao.mydao1"> <property name="datasource" ref="datasource" /> </bean> <bean id="mydao2" class="com.test.dao.mydao2"> <property name="datasource" ref="datasource" /> </bean> <bean id="mycontroller" class="com.test.controller.mycontroller"> <property name="mydao1" ref="mydao1" /> <property name="mydao2" ref="mydao2" /> </bean> <context:component-scan base-package="com.test.controller.mycontroller" /> <mvc:annotation-driven />
now error
java.lang.illegalstateexception: ambiguous mapping found. cannot map 'mycontroller' bean method public com.test.entity.mytest com.test.controller.mycontroller.getgreeting() {[//name],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}: there 'mycontroller' bean method public com.test.entity.mytest com.test.controller.mycontroller.getgreeting() mapped.
when remove requestmapping annotations can deploy on tomcat don't know how call service
org.springframework.web.servlet.dispatcherservlet nohandlerfound warning: no mapping found http request uri [/mytestservice/getgreeting] in dispatcherservlet name 'rest'
'rest' servlet map in web.xml
what missing?
you loading controller twice, , spring tries map twice :
- it declared in
rest-servlet.xml
- it found annotated controller, due
@restcontroller
annotation , tag<context:component-scan base-package="de.cat.auftrag.controller" />
inrest-servlet.xml
you should either :
- remove declaration
<bean id="mycontroller" ...
rest-servlet.xml
, have autowire daos - remove
<context:component-scan ...
tag, if not have other bean scan.
Comments
Post a Comment