【问题标题】:How to parse Spring Integration HTTP gateway response into a Bean?如何将 Spring Integration HTTP 网关响应解析为 Bean?
【发布时间】:2015-08-18 12:10:35
【问题描述】:

出于练习目的,我一直在尝试获得一个简单的集成工作流程。问题是我刚开始使用 Spring,我很难理解集成及其工作原理。

现在我有一个非常简单的带有 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)数组。

如果我不向outbound-gateway 添加reply-channel,一切都会正常工作。但是当我将它发送到另一个频道以将其解析为 Series 对象时,我开始在新频道上收到“调度程序没有订阅者”。这是有道理的,但它让我现在不知道如何进行。

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

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

请求网关

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

集成组件.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>

【问题讨论】:

    标签: spring spring-integration


    【解决方案1】:

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

    我看到了你的@Gateway 配置,但它有点错误。 XML 配置优先于注释。因此,最后的requestChannel 就是inputChannel

    如果您想将 &lt;map-to-object-transformer&gt; 的结果发送到 responseChannel 并接受来自 RequestGateway 调用的 return,您应该在网关配置。

    从另一面看,您根本不需要它,TemporaryReplyChannel 来救援。

    更多信息请参考 Spring Integration Manual

    【讨论】:

    • 看来我误解了网关的工作原理,这就是让我无法去任何地方的原因。我确实收到了 inputChannel。在正确解析网关方法后设置reply-channel=responseChannel(最后我决定将其解析为数组而不是列表,并且必须进行适当的更改)就可以了。对不起,我花了这么长时间来回答。我想先确保一切正常。
    猜你喜欢
    • 2016-03-30
    • 1970-01-01
    • 2020-10-13
    • 1970-01-01
    • 2012-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多