【问题标题】:Issue with application/json reponse being escaped in Spring Integration 4.2在 Spring Integration 4.2 中转义应用程序/json 响应的问题
【发布时间】:2016-02-28 16:27:34
【问题描述】:

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

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

@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端点返回

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

当通过 Spring Integration 的调用返回时

"{\"附属\":\"测试\",\"生产者\":\"TST\",\"产品\":[{\"

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

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

【问题讨论】:

    标签: java json spring spring-mvc spring-integration


    【解决方案1】:

    最好的猜测是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;它适用于我的入站 accepttext/plainapplication/json

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

    {"foo":"bar"} 已被我的客户接收。

    如果您无法使用 DEBUG 日志记录和/或调试器解决此问题,您可以尝试仅使用 StringHttpMessageConverter 重新配置入站网关。

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

    【讨论】:

    • 我只为入站和出站切换到 StringHttpMessageConverter,并使用我想要的所有 MediaType 变体调用 setSupportedMediaTypes()。现在我遇到了 contentType 问题jira.spring.io/browse/INT-3508 并正在解决这个问题。如果我不能弄清楚,我会问另一个问题......
    猜你喜欢
    • 2013-03-14
    • 1970-01-01
    • 1970-01-01
    • 2014-03-02
    • 2018-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-30
    相关资源
    最近更新 更多