Special
ResponseErrorHandler
to be used with the
OAuth2RestTemplate
Note: When using Springs OAuth2 RestTemplate an
OAuth2ErrorHandler will be registered on the
RestTemplate
, which wraps the actual one. This error handler may call the actual error handler
although it does not deem the response as an error. Snippet below shows that
OAuth2ErrorHandler
deems all 4xx response codes as an error and will call the actual handler regardless of its behavior.
public boolean hasError(ClientHttpResponse response) throws IOException {
return HttpStatus.Series.CLIENT_ERROR.equals(response.getStatusCode().series())
|| this.errorHandler.hasError(response);
}
As the
OAuth2ErrorHandler will have the response already consumed at this point, it passes a
buffered response to the actual error handler. Therefore the only chance to process the response is to
process the buffered response of the actual error handler.
To do so this error handler propagates the buffered response back to the
Dispatcher
for dispatching
by throwing an exception containing the response.
Rest
catches this exception and continues with
the normal execution path.