我一直在嘗試爲實踐目的獲得一個簡單的集成工作流程。事情是我剛剛開始與Spring合作,而且我很難理解集成及其工作原理。如何解析Spring集成HTTP網關響應到Bean中?
現在我已經得到了與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項目,與整合,嘗試恢復數據,並將其轉換爲Series
(具有與JSON相同屬性的bean)數組。
如果我沒有添加reply-channel
到outbound-gateway
一切正常。但是,當我將它發送到另一個頻道以解析它到一個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);
}
}
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>
它看起來像我誤解了網關的工作方式,這就是讓我無法去任何地方。我的確在收到inputChannel。在正確解析了網關的方法之後,設置了「reply-channel = responseChannel」(最終我決定把它解析成一個數組而不是一個列表,並且必須做適當的修改)。對不起,我花了這麼長時間來回答。我想確保在我做之前一切正常。 –