我们正在使用spring-integation(基于xml的配置),在其中执行以下步骤
<int:object-to-json-transformer content-type="test.Request" input-channel="objectToJsonChannel" output-channel="apiChannel"/>
<int-http:outbound-gateway id="apiChannel"
request-channel="apiChannel"
reply-channel="jsonToObjectChannel"
....
/>
<int:json-to-object-transformer type="test.Response" input-channel="jsonToObjectChannel" output-channel="responseChannel"/>
以上代码在Spring集成版本5.1之前有效。当我升级到5.2时。它开始抛出异常为org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [test.Request] to type [test.Response]
。
我注意到object-to-json-transformer
在表头上使用键json__TypeId__
添加类类型。然后,它将该类类型用于json-to-object-transformer
。
但是如果提到的话,应该使用json-to-object-transformer
上提到的type属性。
请提出解决此问题的建议,或者在Spring集成(5.2)中是否确实存在错误。
考虑在调用REST服务之前添加<header-filter header-names="json_*" />
。 <int:object-to-json-transformer>
填充JsonHeaders
,以让下游知道我们在有效负载中使用的JSON的真正类型。
A <int:json-to-object-transformer>
更喜欢这些标头,而不是静态type
选项。但是,由于payload
已经是与那些请求标头不同的表示形式,所以它做错了。
我建议在<int:json-to-object-transformer>
上使用一个选项进行偏好设置,但这并不是完全合乎逻辑的。由于我们已经更改了有效负载,因此最好更改其相应的标头。否则,我们只是对自己撒谎。
另一方面,HTTP出站网关可以帮助您将请求转换为用于网络的JSON,并从JSON响应转换回某种POJO类型。
请参见https://docs.spring.io/spring-integration/docs/5.2.3.RELEASE/reference/html/http.html#http-outbound及其expected-response-type
。只要contentType
标头是application/json
,就可以避免使用<int:object-to-json-transformer>
和<int:json-to-object-transformer>
。