如何在建议恢复回调中获取 HTTP 状态
How to get HTTP status in advice recovery callback
我有一个简单的集成流程,它通过多次尝试向外部资源发送请求。为此,我正在创建 RequestHandlerRetryAdvice
:
RequestHandlerRetryAdvice requestHandlerRetryAdvice = new RequestHandlerRetryAdvice();
requestHandlerRetryAdvice.setRecoveryCallback(retryContext -> {
Message<?> failedMessage = ((MessagingException) retryContext.getLastThrowable()).getFailedMessage();
//TODO: get http status
return failedMessage.getPayload();
});
我需要知道的是http状态。我怎样才能得到它?
HTTP 状态是抛出异常的一部分:
((MessagingException) retryContext.getLastThrowable()).getCause()
应该包含 HttpStatusCodeException
和 getStatusCode()
属性.
我的测试显示:
retryContext = ...
lastException = org.springframework.messaging.MessagingException: ...
failedMessage = ...
backtrace = {Object[6]@7673}
detailMessage = "Failed to handle"
cause = org.springframework.messaging.MessageHandlingException: ...
failedMessage = ...
cause = {HttpClientErrorException$Forbidden@7685} "org.springframework.web.client.HttpClientErrorException$Forbidden: 403 Forbidden: [no body]"
statusCode = {HttpStatus@7690} "403 FORBIDDEN"
rawStatusCode = 403
statusText = "Forbidden"
代码是这样的:
.handle(Http.outboundGateway("/service/internal?{params}")
.uriVariable("params", "payload")
.expectedResponseType(String.class),
e -> e.advice(retryAdvice()))
private RequestHandlerRetryAdvice retryAdvice() {
RequestHandlerRetryAdvice requestHandlerRetryAdvice = new RequestHandlerRetryAdvice();
requestHandlerRetryAdvice.setRecoveryCallback(retryContext -> {
MessagingException lastThrowable = (MessagingException) retryContext.getLastThrowable();
return ((HttpStatusCodeException) lastThrowable.getCause().getCause()).getStatusCode();
});
return requestHandlerRetryAdvice;
}
我有一个简单的集成流程,它通过多次尝试向外部资源发送请求。为此,我正在创建 RequestHandlerRetryAdvice
:
RequestHandlerRetryAdvice requestHandlerRetryAdvice = new RequestHandlerRetryAdvice();
requestHandlerRetryAdvice.setRecoveryCallback(retryContext -> {
Message<?> failedMessage = ((MessagingException) retryContext.getLastThrowable()).getFailedMessage();
//TODO: get http status
return failedMessage.getPayload();
});
我需要知道的是http状态。我怎样才能得到它?
HTTP 状态是抛出异常的一部分:
((MessagingException) retryContext.getLastThrowable()).getCause()
应该包含 HttpStatusCodeException
和 getStatusCode()
属性.
我的测试显示:
retryContext = ...
lastException = org.springframework.messaging.MessagingException: ...
failedMessage = ...
backtrace = {Object[6]@7673}
detailMessage = "Failed to handle"
cause = org.springframework.messaging.MessageHandlingException: ...
failedMessage = ...
cause = {HttpClientErrorException$Forbidden@7685} "org.springframework.web.client.HttpClientErrorException$Forbidden: 403 Forbidden: [no body]"
statusCode = {HttpStatus@7690} "403 FORBIDDEN"
rawStatusCode = 403
statusText = "Forbidden"
代码是这样的:
.handle(Http.outboundGateway("/service/internal?{params}")
.uriVariable("params", "payload")
.expectedResponseType(String.class),
e -> e.advice(retryAdvice()))
private RequestHandlerRetryAdvice retryAdvice() {
RequestHandlerRetryAdvice requestHandlerRetryAdvice = new RequestHandlerRetryAdvice();
requestHandlerRetryAdvice.setRecoveryCallback(retryContext -> {
MessagingException lastThrowable = (MessagingException) retryContext.getLastThrowable();
return ((HttpStatusCodeException) lastThrowable.getCause().getCause()).getStatusCode();
});
return requestHandlerRetryAdvice;
}