HTTP-Outbound网关未显示特殊字符 - 编码中断

时间:2012-10-11 18:18:36

标签: spring http character-encoding spring-integration

我们有一个使用http-outboundgateway进行REST调用的客户端

<int-http:outbound-gateway request-channel="glNewsRequestChannel"
url="${gl.url}" http-method="GET" expected-response-type="java.lang.String"
reply-channel="glHeaderEnricher" charset="iso-8859-1">
<int-http:uri-variable name="site_code"
expression="payload" />
</int-http:outbound-gateway>

当获取并保存记录在文件中的响应时,它会显示一些混乱的字符 - 基本上无法转换特定的编码。 我提到了SI文档,除非指定了工厂,否则使用Java URLconnection类来放置REST调用。 为了缩小这个问题,我编写了一个小型Java程序并直接使用了URLconnection类,而没有使用任何开箱即用的模板或网关 - 成功获取并呈现所有特殊字符。我尝试了另一个apache http库的独立应用程序,它也能够获取该角色。 我作为SI的一部分缺少的任何配置?

1 个答案:

答案 0 :(得分:2)

进一步的调查显示我们使用了错误的消息转换器 - 如果我们使用byte []而不是使用String转换器,则按照bytearraymessageconverter -it按预期工作。

    <int-http:outbound-gateway request-channel="glNewsRequestChannel" url="${gl.url}" 
    http-method="GET" message-converters="byteArrayHttpMessageConverter" 
    expected-response-type="byte[]" reply-channel="glHeaderEnricher" 
    charset="iso-8859-1">  <int-http:uri-variable name="site_code" 
    expression="payload" /> 
   </int-http:outbound-gateway>

其中byteArrayHttpMessageConverter可以定义为:

    <bean id="byteArrayHttpMessageConverter"
    class="org.springframework.http.converter.ByteArrayHttpMessageConverter">
    </bean>