spring XD:如何创建自定义http源

时间:2016-05-06 08:46:02

标签: spring spring-mvc spring-xd

我需要实现具有交叉原点的自定义http源:*。

怎么做? Spring XD Doc

需要示例

2 个答案:

答案 0 :(得分:1)

开发自定义源需要您具备有关spring集成的基本知识。无论如何这里是一个例子:

您需要写4个文件:

文件1:

crossoriginsource.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:int="http://www.springframework.org/schema/integration"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
        http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-4.2.xsd">

    <context:component-scan base-package="com.yourbasepackage"></context:component-scan>
    <context:annotation-config></context:annotation-config>

    <!-- The name of the chaannel is output.(Mandatory to have the name as output) -->
    <int:channel id="output"></int:channel>

    <int:inbound-channel-adapter ref="crossOriginHttpCaller" method="makeHttpCall" channel="output"></int:inbound-channel-adapter>
</beans>

crossoriginsource.properties

info.shortDescription = Gets data from cross origin sources.
options_class = com.yourbasepackage.CrossOriginSourceMetadata

CrossOriginSourceMetadata.java

package com.yourbasepackage;

public class CrossOriginSourceMetadata {
    // All the module related configurable properties goes here
    // The properties values for which you would like to take then while defining a stream.
}

CrossOriginHttpCaller.java

@Component
public class CrossOriginHttpCaller {
    public Message<String> makeHttpCall() {
        // your http calling logic goes here
    }
}

开发模块后,必须将其保存在特定的文件夹结构中

crossoriginsource
    |
    |---config
    |       |--crossoriginsource.xml
    |       |--crossoriginsource.properties
    |
    |---lib
    |    |--jar containing CrossOriginHttpCaller.java and CrossOriginSourceMetadata.java
    |    |--all the other dependent jars for this module

完成此结构中的应用程序打包后,您必须将其复制到xd/modules/source,并且必须定义类似

的流。

stream create --name "test" --definition "crossoriginsource | log" --deploy

希望这有帮助。

答案 1 :(得分:0)

需要实现MessageProducerSupport。

使用回复标题:response.setHeader("Access-Control-Allow-Origin", "*");

例如:fork spring-xd-http-extension。

以NettyHttpInboundChannelAdapter为例。