在Spring Integration 4.2中逃脱了application/json响应的问题 [英] Issue with application/json reponse being escaped in Spring Integration 4.2
问题描述
在Spring Integration 4.2.1.RELEASE中创建HTTP代理.环境正在使用最新的2.0.0.RELEASE平台BOM,其中包括运行在Tomcat7上的spring-webmvc层.
Creating an HTTP proxy in Spring Integration 4.2.1.RELEASE. Environment is using the latest 2.0.0.RELEASE platform BOM, including a spring-webmvc layer - running on Tomcat7.
调用是应用程序/json",通过Web层传递到另一个REST服务器端点(setupUrl方法重写URL).该代码成功调用了外部服务器,获得了良好的响应,然后在响应返回给调用者之前对其进行了处理.
Calls are "application/json", passed through the web layer to a different REST server endpoint (the setupUrl method rewrites the URL). The code successfully calls the external server, gets good response, then mangles the response before it returns to the caller.
@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端点的调用会返回
The call directly to the REST endpoint returns
{会员":测试",生产者":"TST",产品" ...
{"affiliate":"test","producer":"TST","products"...
通过Spring Integration进行的调用返回
While the call through Spring Integration returns
"{\"会员\:\"测试\,\"生产者\:\" TST \,\"产品\:[{\"
"{\"affiliate\":\"test\",\"producer\":\"TST\",\"products\":[{\"
尝试了将StringHttpMessageConverter添加到出站适配器的许多组合.混淆编码(UTF-8而不是ISO-8859-1).响应字符串有些混乱,似乎在我可以告诉它之前,它就离Spring Integration很近了. Integration上次接触它的时间是HttpRequestHandlingMessagingGateway.handleRequest()第117行.在该响应对象中,它看起来仍然正确.
Tried a lot of combinations of adding StringHttpMessageConverter to the outbound adapter. Messing with the encodings (UTF-8 rather than ISO-8859-1). Something is messing with the response string, and it seems to be AFTER it leaves Spring Integration as near as I can tell. The last time Integration touches it is HttpRequestHandlingMessagingGateway.handleRequest() line 117. It still looks correct in the response object there.
spring-mvc可能确实存在问题,这是我在调试中首先看到错误的字符串.
It's possible the issue is really with spring-mvc, that is the first place I see the mangled string in debugging.
推荐答案
最好的猜测是accept
(入站)或content-type
(出站)存在一些问题.
Best guess is some problem with the accept
(inbound) or content-type
(outbound).
我只是这样更改了http示例...
I just changed the http sample like so...
<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/plain
或application/json
的入站accept
.
The payload being returned to the first gateway is type String
; it works for me with an inbound accept
of text/plain
or application/json
.
由于StringHttpMessageConverter
在列表中的JSON消息转换器之前,并且有效负载为String
,因此选择它是因为类型为String
并且该转换器可以处理*/*
accept
,因此没有双重JSON编码.
Since the StringHttpMessageConverter
is ahead of the JSON message converter in the list, and the payload is a String
, it is chosen because the type is String
and that converter can handle */*
accept
, so there's no double JSON encoding.
{"foo":"bar"}
.
如果无法通过DEBUG日志记录和/或调试器解决问题,则可以尝试仅使用StringHttpMessageConverter
重新配置入站网关.
If you can't figure it out with DEBUG logging and/or the debugger, you could try reconfiguring the inbound gateway with just a StringHttpMessageConverter
.
在HttpRequestHandlingMessagingGateway
的第151行(当前版本)处设置一个断点,以查看正在选择哪个出站消息转换器.
Set a breakpoint at line 151 (current release) in HttpRequestHandlingMessagingGateway
to see which outbound message converter is being selected.
这篇关于在Spring Integration 4.2中逃脱了application/json响应的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!