【问题标题】:Spring REST Service with IOC?带有 IOC 的 Spring REST 服务?
【发布时间】:2014-11-19 16:02:38
【问题描述】:

过去我使用 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 映射

我错过了什么?

【问题讨论】:

  • 请求应该是/MyTestService/name,这是请求映射中映射的内容。

标签: java spring rest spring-mvc


【解决方案1】:

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

  • 在rest-servlet.xml中声明
  • 由于@RestController 注释和rest-servlet.xml 中的标签&lt;context:component-scan base-package="de.cat.auftrag.controller" /&gt; 而发现它是一个带注释的控制器

你应该:

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

【讨论】:

    【解决方案2】:

    为什么要麻烦去做 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;
    }
    

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

    然后删除。

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

    如果你想学习 Spring……你必须选择你的毒药。转到 XML 或注释。将两者混合在一起是一个非常漫长而艰苦的学习过程的秘诀。

    【讨论】:

    • Serge Ballesta 的回答对我有用。但我意识到我现在正在使用 XML 和注释。什么是“更好”的做事方式? XML 还是注解?
    • @Diskutant:见仁见智。控制器中需要注释 (RequestMapping ...)。对于其余部分,两者都同样可用并且完全由 spring 支持。
    • 我正在尝试切换到注释。我用@Autowired 在我的控制器中注释了DAO,它可以工作,但是我的DAO 扩展了NamedParameterJdbcDaoSupport,并且数据源也需要连接。错误是:"No qualifying bean of type [com.test.dao.MyDAO] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency." 如果我在我的 rest-servlet.xml 中有它,它可以正常工作,但是当只使用注释时,除了&lt;context:component-scan base-package="de.cat.auftrag.controller" /&gt; &lt;mvc:annotation-driven /&gt; 之外应该没有任何东西,对吗?
    • @Bean 覆盖你的 DAO 类
    • 恕我直言,我会将您的数据源 bean 保留在您的 servlet/应用程序上下文中的 XML 中。无论如何,90% 的关于 spring 的教程都是这样做的。
    猜你喜欢
    • 2017-01-10
    • 2015-08-16
    • 2017-12-15
    • 2013-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多