我们称这种情况为HTTP代理和对此事的一些测试用例:https://github.com/spring-projects/spring-integration/blob/master/spring-integration-http/src/test/java/org/springframework/integration/http/dsl/HttpDslTests.java
我只是做了一些调查,以下是你需要什么:
IntegrationFlows
.from(Http.inboundGateway("/service")
.requestMapping(r -> r.params("name"))
.errorChannel("httpProxyErrorFlow.input"))
...
@Bean
public IntegrationFlow httpProxyErrorFlow() {
return f -> f
.transform(Throwable::getCause)
.<HttpClientErrorException>handle((p, h) ->
MessageBuilder.withPayload(p.getResponseBodyAsString())
.setHeader(HttpHeaders.STATUS_CODE, p.getStatusCode())
.build());
}
我们必须处理入站网关级别的下游错误。为此,Spring Integration建议使用errorChannel
功能。
httpProxyErrorFlow
提供了一些关于此事的逻辑。首先我们知道errorChannel
的讯息是ErrorMessage
,而其是MessagingException
- 作为将HttpClientErrorException
包装在HttpRequestExecutingMessageHandler
中的结果。因此,通过.transform()
我们可以深入了解所需的例外情况,并在.handle()
中根据HttpClientErrorException
内容构建新消息。
的HttpRequestHandlingMessagingGateway
能够正确地处理这样的消息和期望的状态代码设置为响应:
protected final Object setupResponseAndConvertReply(ServletServerHttpResponse response, Message<?> replyMessage) {
getHeaderMapper().fromHeaders(replyMessage.getHeaders(), response.getHeaders());
HttpStatus httpStatus = this.resolveHttpStatusFromHeaders(replyMessage.getHeaders());
if (httpStatus != null) {
response.setStatusCode(httpStatus);
}
http://docs.spring.io/spring-integration/docs/4.3.11.RELEASE/reference/html/http.html#http-response-statuscode
请,分享堆栈跟踪您看到有错误之前为您服务,即500 –