【问题标题】:how does outbound-gateway handle json string from remote serveroutbound-gateway 如何处理来自远程服务器的 json 字符串
【发布时间】:2014-11-26 06:08:03
【问题描述】:

我是 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:application/json;charset=UTF-8 的 JSON

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

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

【问题讨论】:

  • 你可以使用网络嗅探器(wireshark 或 eclipse 的 tcp/ip 监视器等)来查看实际内容吗?
  • 嗨,远程服务器返回整洁的 json,我直接调用它。编码或转换肯定有问题...

标签: json spring-integration


【解决方案1】:

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

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

  2. 使用HttpComponentsClientHttpRequestFactory 注入&lt;int-http:outbound-gateway&gt;DecompressingHttpClient 装饰器引用。

我认为第二个更好。

【讨论】:

    【解决方案2】:

    似乎问题是远程响应被压缩了,并且被转换的字符串已经错误,所以我无法解压缩它。我必须设置 expected-response-type="byte[]" 以便我可以通过代码解压缩它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-29
      • 2018-06-21
      • 2018-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-06
      • 2013-10-14
      相关资源
      最近更新 更多