outbound-gateway如何处理来自远程服务器的json字符串

时间:2014-11-26 06:08:04

标签: json spring-integration

我是Spring集成的新手,我正在用它开发代理。

这是我的配置:

<int-http:inbound-gateway id="testInboundGateway"
                          supported-methods="GET"
                          request-channel="test-request"
                          extract-reply-payload="false"
                          reply-channel="test-reply"
                          path="/user/{id}/info"
                          reply-timeout="50000">
    <int-http:header name="id" expression="#pathVariables.id"/>
</int-http:inbound-gateway>


<int-http:outbound-gateway id="testOutboundGateway"
                           http-method="GET"
                           request-channel="test-request"
                           reply-channel="test-process"
                           url="http://remoteserver/{id}.json"
                           extract-request-payload="false"
                           expected-response-type="java.lang.String"
                           reply-timeout="50000"
                           charset="UTF-8">
    <int-http:uri-variable name="id" expression="headers.id" />
</int-http:outbound-gateway>

<int:service-activator id="activator"
                       input-channel="test-process"
                       output-channel="test-reply"
                       ref="hubBean"
                       method="process"></int:service-activator>

<bean id="hubBean" class="com.test.testhub.HubService"/>

我的远程服务器使用content-type返回JSON:application / json; charset = UTF-8

但是我的服务bean得到这样的字符串:\ u001F \ b \ u0000 \ u0000 .....

我不知道它是什么。我尝试用gzip解码它,但格式不正确。 所以任何人都可以告诉我如何才能得到实际结果?

2 个答案:

答案 0 :(得分:0)

看起来问题是远程响应被gzip压缩,并且已经错误的String已经出错,因此我无法解压缩它。我必须设置expected-response-type =&#34; byte []&#34;所以我可以通过代码解压缩它。

答案 1 :(得分:0)

由于我们已经知道您对该远程服务的响应是gzipped,即使它是JSON,也有两种方法可以解决这个问题并提供强大的解决方案:

  1. 接受响应为byte[](您现在就这样做)并手动解压缩。

  2. HttpComponentsClientHttpRequestFactory <int-http:outbound-gateway>使用DecompressingHttpClient装饰器参考,使用{{1}}注入。

  3. 我认为第二个更好。