application/json 响应在 Spring Integration 4.2 中被转义的问题

Issue with application/json reponse being escaped in Spring Integration 4.2

在 Spring Integration 4.2.1.RELEASE 中创建 HTTP 代理。环境正在使用最新的 2.0.0.RELEASE 平台 BOM,包括一个 spring-webmvc 层 - 运行 on Tomcat7.

调用 "application/json",通过 Web 层传递到不同的 REST 服务器端点(setupUrl 方法重写 URL)。代码成功调用外部服务器,获得良好的响应,然后将它之前的响应 returns 破坏给调用者。

@Bean
    public IntegrationFlow httpProxyFlow()  {
        return IntegrationFlows
            .from((MessagingGateways g) ->
                    g.httpGateway("/my-service/**")
                            .messageConverters(new MappingJackson2HttpMessageConverter())
                        .payloadFunction(httpEntity ->
                                ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
                                        .getRequest()
                                        .getQueryString())
                        .requestPayloadType(String.class))
                        .handleWithAdapter(a ->
                                a.httpGateway(this::setupUrl)
                                        .httpMethodFunction(this::getMethodFunction)
                                        .errorHandler(new PassThroughErrorHandler())
                                        .encodeUri(false)
                                        .expectedResponseType(String.class)
                        ).get();
    }

直接调用 REST 端点returns

{"affiliate":"test","producer":"TST","products"...

同时调用通过Spring集成returns

"{\"affiliate\":\"test\",\"producer\":\"TST\",\"products\":[{\"

尝试了很多将 StringHttpMessageConverter 添加到出站适配器的组合。搞乱编码(UTF-8 而不是 ISO-8859-1)。响应字符串出现问题,据我所知似乎是在它离开 Spring 集成之后。 Integration 最后一次触及它是 HttpRequestHandlingMessagingGateway.handleRequest() 第 117 行。它在那里的响应对象中看起来仍然正确。

问题可能真的出在 spring-mvc 上,这是我在调试中看到损坏的字符串的第一个地方。

最好的猜测是 accept(入站)或 content-type(出站)有问题。

我只是像这样更改了 http 示例...

<int-http:inbound-gateway request-channel="proxyChannel"
                            reply-channel="reply"
                            path="/receiveGateway"
                            supported-methods="POST"/>

<int:channel id="reply" />

<int-http:outbound-gateway request-channel="proxyChannel"
                reply-channel="reply"
                expected-response-type="java.lang.String"
                url="http://localhost:8080/http/receiveGateway2"/>

<int-http:inbound-gateway request-channel="receiveChannel"
                            path="/receiveGateway2"
                            supported-methods="POST"/>

<int:channel id="receiveChannel"/>

<int:chain input-channel="receiveChannel">
    <int:header-filter header-names="content-type" />
    <int:service-activator expression='{"foo" : "bar"}'/>
</int:chain>

返回到第一个网关的负载类型为String;它适用于 text/plainapplication/json.

的入站 accept

由于 StringHttpMessageConverter 在列表中位于 JSON 消息转换器之前,并且有效载荷是 String,因此选择它是因为类型是 String该转换器可以处理 */* accept,因此没有双重 JSON 编码。

{"foo":"bar"} 我的客户收到了。

如果您无法通过 DEBUG 记录 and/or 调试器解决问题,您可以尝试仅使用 StringHttpMessageConverter.

重新配置入站网关

HttpRequestHandlingMessagingGateway 的第 151 行(当前版本)设置断点,以查看正在选择哪个出站消息转换器。