Spring Integration Http.outboundGateway解析失败时如何获取原始响应?
Spring Integration HTTP网关解析失败时获取原始响应
场景与问题
使用Spring Integration HTTP出站网关调用接口,预期接收Something.class对应的JSON格式响应,但受数据检查防火墙影响,接口可能拒绝返回JSON,转而返回完全不同的响应(如XML)。此时error_channel的堆栈跟踪仅会报告请求URI和解析失败原因,无法获取到原始的响应内容。
原始代码如下:
.handle( Http .outboundGateway("http://localhost/something") .httpMethod(HttpMethod.GET) .expectedResponseType(Something.class) ) .<Something>handle((p, h) -> { .... })
当前解决方案
目前的处理方式是将expectedResponseType指定为String.class,在后续的handle处理器中,使用com.fasterxml.jackson.databind.ObjectMapper.readValue(payload, Something.class)手动解析响应。若解析抛出异常,则记录原始负载内容并返回null。
疑问
是否可以直接指定Something.class作为目标响应类型,同时在解析失败时获取到网关接收到的原始响应内容?
编辑1:尝试请求处理器通知后的问题
参考Spring Integration相关文档示例修改代码,为网关添加了ExpressionEvaluatingRequestHandlerAdvice通知:
.handle( Http .outboundGateway("http://localhost/something") .httpMethod(HttpMethod.GET) .expectedResponseType(Something.class) , a -> a.advice(myAdvice()) ) .<Something>handle((p, h) -> { .... }) @Bean public Advice myAdvice() { ExpressionEvaluatingRequestHandlerAdvice eerha = new ExpressionEvaluatingRequestHandlerAdvice(); eerha.setOnFailureExpressionString("payload + ' was BAAAAAAD ' + #exception.cause.message"); eerha.setFailureChannelName("baaad"); eerha.setTrapException(true); return eerha; }
但实际测试发现,通知返回的evaluationResult和failedMessage指向的都是HTTP出站网关的输入消息,无法获取到网关在尝试按expectedResponseType编组失败前接收到的原始输出响应。
内容的提问来源于stack exchange,提问作者lafual
相关产品推荐
相关产品推荐

