Class ExtractingResponseErrorHandler
- All Implemented Interfaces:
ResponseErrorHandler
ResponseErrorHandler
that uses HttpMessageConverters
to convert HTTP error responses to RestClientExceptions
.
To use this error handler, you must specify a
status mapping and/or a
series mapping. If either of these mappings has a match
for the status code of a given
ClientHttpResponse
, DefaultResponseErrorHandler.hasError(ClientHttpResponse)
will return
true
, and DefaultResponseErrorHandler.handleError(ClientHttpResponse)
will attempt to use the
configured message converters to convert the response
into the mapped subclass of RestClientException
. Note that the
status mapping takes precedence over
series mapping.
If there is no match, this error handler will default to the behavior of
DefaultResponseErrorHandler
. Note that you can override this default behavior
by specifying a series mapping from
HttpStatus.Series#CLIENT_ERROR
and/or HttpStatus.Series#SERVER_ERROR
to null
.
- Since:
- 5.0
- Author:
- Simon Galperin, Arjen Poutsma
- See Also:
-
Constructor Summary
ConstructorDescriptionCreate a new, emptyExtractingResponseErrorHandler
.ExtractingResponseErrorHandler
(List<HttpMessageConverter<?>> messageConverters) Create a newExtractingResponseErrorHandler
with the givenHttpMessageConverter
instances. -
Method Summary
Modifier and TypeMethodDescriptionvoid
handleError
(URI url, HttpMethod method, ClientHttpResponse response) Handle the error in the given response with the given resolved status code and extra information providing access to the request URL and HTTP method.protected void
handleError
(ClientHttpResponse response, HttpStatusCode statusCode, URI url, HttpMethod method) Handle the error based on the resolved status code.protected boolean
hasError
(HttpStatusCode statusCode) Template method called fromDefaultResponseErrorHandler.hasError(ClientHttpResponse)
.void
setMessageConverters
(List<HttpMessageConverter<?>> messageConverters) Set the message converters to use by this extractor.void
setSeriesMapping
(Map<HttpStatus.Series, Class<? extends RestClientException>> seriesMapping) Set the mapping from HTTP status series toRestClientException
subclass.void
setStatusMapping
(Map<HttpStatusCode, Class<? extends RestClientException>> statusMapping) Set the mapping from HTTP status code toRestClientException
subclass.Methods inherited from class org.springframework.web.client.DefaultResponseErrorHandler
getCharset, getResponseBody, handleError, hasError, hasError, initBodyConvertFunction
-
Constructor Details
-
ExtractingResponseErrorHandler
public ExtractingResponseErrorHandler()Create a new, emptyExtractingResponseErrorHandler
.Note that
setMessageConverters(List)
must be called when using this constructor. -
ExtractingResponseErrorHandler
Create a newExtractingResponseErrorHandler
with the givenHttpMessageConverter
instances.- Parameters:
messageConverters
- the message converters to use
-
-
Method Details
-
setMessageConverters
Set the message converters to use by this extractor. -
setStatusMapping
public void setStatusMapping(Map<HttpStatusCode, Class<? extends RestClientException>> statusMapping) Set the mapping from HTTP status code toRestClientException
subclass. If this mapping has a match for the status code of a givenClientHttpResponse
,DefaultResponseErrorHandler.hasError(ClientHttpResponse)
will returntrue
andDefaultResponseErrorHandler.handleError(ClientHttpResponse)
will attempt to use the configured message converters to convert the response into the mapped subclass ofRestClientException
. -
setSeriesMapping
public void setSeriesMapping(Map<HttpStatus.Series, Class<? extends RestClientException>> seriesMapping) Set the mapping from HTTP status series toRestClientException
subclass. If this mapping has a match for the status code of a givenClientHttpResponse
,DefaultResponseErrorHandler.hasError(ClientHttpResponse)
will returntrue
andDefaultResponseErrorHandler.handleError(ClientHttpResponse)
will attempt to use the configured message converters to convert the response into the mapped subclass ofRestClientException
. -
hasError
Description copied from class:DefaultResponseErrorHandler
Template method called fromDefaultResponseErrorHandler.hasError(ClientHttpResponse)
.The default implementation checks
HttpStatusCode.isError()
. Can be overridden in subclasses.- Overrides:
hasError
in classDefaultResponseErrorHandler
- Parameters:
statusCode
- the HTTP status code- Returns:
true
if the response indicates an error;false
otherwise- See Also:
-
handleError
Description copied from class:DefaultResponseErrorHandler
Handle the error in the given response with the given resolved status code and extra information providing access to the request URL and HTTP method.The default implementation throws:
HttpClientErrorException
if the status code is in the 4xx series, or one of its sub-classes such asHttpClientErrorException.BadRequest
and others.HttpServerErrorException
if the status code is in the 5xx series, or one of its sub-classes such asHttpServerErrorException.InternalServerError
and others.UnknownHttpStatusCodeException
for error status codes not in theHttpStatus
enum range.
- Specified by:
handleError
in interfaceResponseErrorHandler
- Overrides:
handleError
in classDefaultResponseErrorHandler
- Parameters:
url
- the request URLmethod
- the HTTP methodresponse
- the response with the error- Throws:
IOException
- in case of I/O errors- See Also:
-
handleError
protected void handleError(ClientHttpResponse response, HttpStatusCode statusCode, @Nullable URI url, @Nullable HttpMethod method) throws IOException Description copied from class:DefaultResponseErrorHandler
Handle the error based on the resolved status code.The default implementation delegates to
HttpClientErrorException.create(org.springframework.http.HttpStatusCode, java.lang.String, org.springframework.http.HttpHeaders, byte[], java.nio.charset.Charset)
for errors in the 4xx range, toHttpServerErrorException.create(org.springframework.http.HttpStatusCode, java.lang.String, org.springframework.http.HttpHeaders, byte[], java.nio.charset.Charset)
for errors in the 5xx range, or otherwise raisesUnknownHttpStatusCodeException
.- Overrides:
handleError
in classDefaultResponseErrorHandler
- Throws:
IOException
- See Also:
-
HttpClientErrorException.create(org.springframework.http.HttpStatusCode, java.lang.String, org.springframework.http.HttpHeaders, byte[], java.nio.charset.Charset)
HttpServerErrorException.create(org.springframework.http.HttpStatusCode, java.lang.String, org.springframework.http.HttpHeaders, byte[], java.nio.charset.Charset)
-