Reputation: 315
In the past I did some simple SOAP Services using Spring and IOC (application-context.xml where I state that my controller has 2 different DAOs etc. and Spring sets them for me)
Now I am trying to do a REST Service using Spring.
My Controller looks like 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 to plugin JSON as request and 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 to convert JSON to POJO and 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 I get the error
java.lang.IllegalStateException: Ambiguous mapping found. Cannot map 'myController' bean method
public com.test.entity.MyTest com.test.controller.MyController.getGreeting()
to {[//name],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}: There is already 'myController' bean method
public com.test.entity.MyTest com.test.controller.MyController.getGreeting() mapped.
When I remove the RequestMapping annotations I can deploy it on the tomcat but I don't know how to call the service
org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/MyTestService/getGreeting] in DispatcherServlet with name 'rest'
'rest' is the servlet map in the web.xml
What am I missing?
Upvotes: 0
Views: 1132
Reputation: 148890
You are loading you controller twice, and Spring tries to map it twice :
rest-servlet.xml
@RestController
annotation and the tag <context:component-scan base-package="de.cat.auftrag.controller" />
in rest-servlet.xml
You should either :
<bean id="myController" ...
from rest-servlet.xml
, but you will have to autowire the DAOs<context:component-scan ...
tag, if you do not have any other bean to scan.Upvotes: 3
Reputation: 3421
Why go through the trouble of doing things that Spring has already done for you?
@Controller // << See Here
@RequestMapping("/")
public class MyController {
@Autowired // << See Here
private MyDAO1 myDAO1;
@Autowired // << See Here
private MyDAO2 myDAO2;
@RequestMapping("/name")
public MyTest getGreeting() {
MyTest tst = new MyTest(1, "Hallo ");
return tst;
}
Your url is most likely the last package in your base package. In this case
http://url:8080/rest/controller
And remove.
<bean id="myController" class="com.test.controller.MyController">
<property name="myDAO1" ref="myDAO1" />
<property name="myDAO2" ref="myDAO2" />
</bean>
If you're going to want to learn Spring... you'll have to pick your poison. Go XML or Annotations. Mixing the two is a recipe for a very very long and grueling learning process.
Upvotes: 1