在Spring Integration 4.2.1.RELEASE中创建HTTP代理。环境使用最新的2.0.0.RELEASE平台BOM,包括在Tomcat7上运行的Spring-webmvc层。
调用是"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端点的调用返回
{"附属公司":"测试","生产商":"TST","产品"…
而通过Spring Integration的调用返回
"{\"联盟\":\"测试\",\"生产商\":"TST\","产品\":[{\"
尝试了许多将StringHttpMessageConverter添加到出站适配器的组合。编码混乱(UTF-8而不是ISO-8859-1)。响应字符串出现了问题,据我所知,似乎是在它离开Spring Integration之后。Integration最后一次接触它是HttpRequestHandlingMessagingGateway.handleRequest()行117。它在那里的响应对象中看起来仍然正确。
问题可能真的与springmvc有关,这是我在调试中第一次看到损坏的字符串。
最佳猜测是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
;它对于CCD_ 5或CCD_ 6的入站CCD_。
由于StringHttpMessageConverter
在列表中领先于JSON消息转换器,并且有效载荷是String
,因此选择它是因为类型是String
,并且该转换器可以处理*/*
accept
,因此不存在双重JSON编码。
我的客户收到了{"foo":"bar"}
。
如果您无法通过DEBUG日志和/或调试器来解决问题,您可以尝试仅使用StringHttpMessageConverter
重新配置入站网关。
在HttpRequestHandlingMessagingGateway
中的第151行(当前版本)设置一个断点,查看正在选择哪个出站消息转换器。