public class ExtractingResponseErrorHandler extends DefaultResponseErrorHandler
ResponseErrorHandler
that uses HttpMessageConverter
s
to convert HTTP error responses to RestClientException
.
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
.
RestTemplate.setErrorHandler(ResponseErrorHandler)
Constructor and Description |
---|
ExtractingResponseErrorHandler()
Create a new, empty
ExtractingResponseErrorHandler . |
ExtractingResponseErrorHandler(List<HttpMessageConverter<?>> messageConverters)
Create a new
ExtractingResponseErrorHandler with the given
HttpMessageConverter instances. |
Modifier and Type | Method and Description |
---|---|
void |
handleError(ClientHttpResponse response,
HttpStatus statusCode)
Handle the error in the given response with the given resolved status code.
|
protected boolean |
hasError(HttpStatus statusCode)
Template method called from
DefaultResponseErrorHandler.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 to
RestClientException subclass. |
void |
setStatusMapping(Map<HttpStatus,Class<? extends RestClientException>> statusMapping)
Set the mapping from HTTP status code to
RestClientException subclass. |
getCharset, getHttpStatusCode, getResponseBody, handleError, hasError, hasError
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
handleError
public ExtractingResponseErrorHandler()
ExtractingResponseErrorHandler
.
Note that setMessageConverters(List)
must be called when using this constructor.
public ExtractingResponseErrorHandler(List<HttpMessageConverter<?>> messageConverters)
ExtractingResponseErrorHandler
with the given
HttpMessageConverter
instances.messageConverters
- the message converters to usepublic void setMessageConverters(List<HttpMessageConverter<?>> messageConverters)
public void setStatusMapping(Map<HttpStatus,Class<? extends RestClientException>> statusMapping)
RestClientException
subclass.
If this mapping 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
.public void setSeriesMapping(Map<HttpStatus.Series,Class<? extends RestClientException>> seriesMapping)
RestClientException
subclass.
If this mapping 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
.protected boolean hasError(HttpStatus statusCode)
DefaultResponseErrorHandler
DefaultResponseErrorHandler.hasError(ClientHttpResponse)
.
The default implementation checks HttpStatus.isError()
.
Can be overridden in subclasses.
hasError
in class DefaultResponseErrorHandler
statusCode
- the HTTP status code as enum valuetrue
if the response indicates an error; false
otherwiseHttpStatus.isError()
public void handleError(ClientHttpResponse response, HttpStatus statusCode) throws IOException
DefaultResponseErrorHandler
The default implementation throws an HttpClientErrorException
if the status code is HttpStatus.Series#CLIENT_ERROR
, an
HttpServerErrorException
if it is HttpStatus.Series#SERVER_ERROR
,
and an UnknownHttpStatusCodeException
in other cases.
handleError
in class DefaultResponseErrorHandler
IOException