IOC的Spring REST服务?

时间:2014-11-19 16:02:39

标签: java spring rest spring-mvc

过去我使用Spring和IOC做了一些简单的SOAP服务(application-context.xml,其中我声明我的控制器有2个不同的DAO等,而Spring为我设置了它们)

现在我正在尝试使用Spring进行REST服务。

我的控制器看起来像这样

@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;
}

我的rest-servlet.xml包含:

<?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 />

现在我收到了错误

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.

当我删除RequestMapping注释时,我可以将它部署在tomcat上,但我不知道如何调用该服务

org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/MyTestService/getGreeting] in DispatcherServlet with name 'rest'

'rest'是web.xml中的servlet映射

我错过了什么?

2 个答案:

答案 0 :(得分:3)

你正在加载控制器两次,而Spring试图将它映射两次:

  • rest-servlet.xml
  • 中声明
  • 由于@RestController注释和<context:component-scan base-package="de.cat.auftrag.controller" />
  • 中的标记rest-servlet.xml,它被作为带注释的控制器找到

你应该:

  • <bean id="myController" ...删除声明rest-servlet.xml,但您必须自动装载DAO
  • 如果您没有要扫描的其他bean,请删除<context:component-scan ...标记。

答案 1 :(得分:1)

为什么要解决Spring已经为你做过的事情呢?

@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;
}

您的网址很可能是基础套餐中的最后一个包。在这种情况下 http://url:8080/rest/controller

然后删除。

<bean id="myController" class="com.test.controller.MyController">
<property name="myDAO1" ref="myDAO1" />
<property name="myDAO2" ref="myDAO2" />
</bean>

如果你想要学习春天......你将不得不挑选毒药。转到XML或注释。混合这两者是一个非常漫长而艰苦的学习过程的配方。