如何将Spring Integration HTTP网关响应解析为Bean?

时间:2015-08-18 12:10:35

标签: spring spring-integration

我一直在努力为实践目的提供一个简单的集成工作流程。问题是我刚刚开始使用Spring,我很难理解Integration及其工作原理。

现在我有一个非常简单的后端应用程序,其中Spring MVC返回

[
{"id":1,"name":"Series test","synopsis":"Testing reading items from JSON.","imageUrl":"http://some.where/images/some_image.png"},
{"id":2,"name":"Arrow","synopsis":"Some guy in a hood shooting arrows to some guys with superpowers.","imageUrl":"http://some.where/images/some_image.png"},
{"id":3,"name":"Primeval","synopsis":"Some guys with guns killing dinosaurs and lots of infidelity.","imageUrl":"http://some.where/images/some_image.png"},
{"id":4,"name":"Dr. Who","synopsis":"It's bigger on the inside.","imageUrl":"http://some.where/images/some_image.png"},
{"id":5,"name":"Fringe","synopsis":"Weird things happen.","imageUrl":"http://some.where/images/some_image.png"},
{"id":6,"name":"Monster Hunter Freedom Unite","synopsis":"Wait. This is a game.","imageUrl":"http://some.where/images/some_image.png"}
]

http://localhost:9000/api/series/findAll和一个可运行的Spring项目,它使用Integration尝试恢复该数据并将其转换为Series(与JSON具有相同属性的bean)数组。

如果我没有向reply-channel添加outbound-gateway,一切正常。但当我将它发送到另一个频道将其解析为Series对象时,我开始得到#34; Dispatcher没有订阅者"在新频道上。这是有道理的,但它让我不知道如何继续。

除了系列之外,我的项目文件现在看起来像这样:

Startup.java

package com.txus.integration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;
import java.util.concurrent.Future;

public class Startup {
    @Autowired
    RequestGateway requestGateway;

    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/META-INF/spring/integration-components.xml");

        RequestGateway requestGateway = context.getBean(RequestGateway.class);
        Future<String> promise = requestGateway.getSeries("");

        while (!promise.isDone()) {
            Thread.sleep(1000);
        }
        String response = promise.get();
        printReadable(response);

        context.close();
        System.exit(0);
    }

    public static void printReadable(String string) {
        String separator = "= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =";
        System.out.println("\n" + separator + "\n" + string + "\n" + separator + "\n");
    }

    public static void printReadable(List<String> strings) {
        for (String string : strings) printReadable(string);
    }
}

RequestGateway

package com.txus.integration;


import com.txus.entities.Series;
import org.springframework.integration.annotation.Gateway;

import java.util.concurrent.Future;

public interface RequestGateway {
    @Gateway(requestChannel="responseChannel")
    Future<String> getSeries(String jsonString);

    @Gateway(requestChannel="responseChannel")
    Future<String> getSeries(Series series);
}

集成-components.xml中

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

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


    <!-- START: Spring Integration -->
    <!-- Integration: Channels -->

    <channel id="requestChannel"/>
    <channel id="responseChannel"/>

    <channel id="failedChannel"/>


    <!-- Integration: Loggers -->

    <logging-channel-adapter
            id="payloadLogger" level="DEBUG" expression="'### Message [' + headers.id + '] payload: ' + payload"/>
    <logging-channel-adapter
            id="headersLogger" level="DEBUG" expression="'### Message [' + headers.id + '] headers: ' + headers"/>


    <!-- Integration: Flow -->

    <gateway
            service-interface="com.txus.integration.RequestGateway"
            default-request-timeout="5000" async-executor="executor">

        <method name="getSeries" request-channel="inputChannel"/>
    </gateway>

    <task:executor id="executor" pool-size="100"/>


    <payload-type-router input-channel="inputChannel" default-output-channel="failedChannel">
        <mapping type="java.lang.String" channel="requestChannel"/>
        <mapping type="com.txus.entities.Series" channel="objectToJSONChannel"/>
    </payload-type-router>


    <object-to-json-transformer
            id="objectToJsonTransformer" input-channel="objectToJSONChannel" output-channel="requestChannel"/>


    <http:outbound-gateway
            http-method="GET"
            expected-response-type="java.lang.String"
            url="http://localhost:9000/api/series/findAll"
            request-channel="requestChannel"
            reply-channel="jsonToSeries"
            reply-timeout="30000"/>


    <map-to-object-transformer
            input-channel="jsonToSeries" output-channel="responseChannel" type="com.txus.entities.Series"/>


    <!-- END: Spring Integration -->

</beans:beans>

1 个答案:

答案 0 :(得分:1)

配置output-channel="responseChannel"的最后一个组件存在问题。没有任何订阅该频道的内容。

我在这件事上看到了你的@Gateway配置,但这有点不对劲。 XML配置优先于那里的注释。因此,requestChannel最终恰好是inputChannel

如果您要将<map-to-object-transformer>的结果发送到responseChannel并从return调用中将其作为RequestGateway接受,则应指定responseChannel 1}}作为网关配置的reply-channel

从另一方面你不需要它,TemporaryReplyChannel来救援。

请参阅Spring Integration Manual的更多信息。