Package org.springframework.web.client

# Class ExtractingResponseErrorHandler

java.lang.Object
org.springframework.web.client.DefaultResponseErrorHandler
org.springframework.web.client.ExtractingResponseErrorHandler

All Implemented Interfaces:
  `ResponseErrorHandler`

public class ExtractingResponseErrorHandler extends DefaultResponseErrorHandler
Implementation of `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 `handleError(ClientHttpResponse, HttpStatusCode, URI, HttpMethod)` 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:
  - `RestTemplate.setErrorHandler(ResponseErrorHandler)`

## Constructor Summary

Constructors
Constructor
Description
`ExtractingResponseErrorHandler()`
Create a new, empty `ExtractingResponseErrorHandler`.
`ExtractingResponseErrorHandler(List<HttpMessageConverter<?>> messageConverters)`
Create a new `ExtractingResponseErrorHandler` with the given `HttpMessageConverter` instances.

## Method Summary

Modifier and Type
Method
Description
`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 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<HttpStatusCode,Class<? extends RestClientException>> statusMapping)`
Set the mapping from HTTP status code to `RestClientException` subclass.

### Methods inherited from class org.springframework.web.client.DefaultResponseErrorHandler

`getCharset, getResponseBody, handleError, handleError, hasError, hasError, initBodyConvertFunction`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### ExtractingResponseErrorHandler

public ExtractingResponseErrorHandler()
Create a new, empty `ExtractingResponseErrorHandler`.

Note that `setMessageConverters(List)` must be called when using this constructor.

### ExtractingResponseErrorHandler

public ExtractingResponseErrorHandler(List<HttpMessageConverter<?>> messageConverters)
Create a new `ExtractingResponseErrorHandler` with the given `HttpMessageConverter` instances.

Parameters:
  `messageConverters` - the message converters to use

## Method Details

### setMessageConverters

public void setMessageConverters(List<HttpMessageConverter<?>> messageConverters)
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 to `RestClientException` subclass. If this mapping has a match for the status code of a given `ClientHttpResponse`, `DefaultResponseErrorHandler.hasError(ClientHttpResponse)` will return `true` and `handleError(ClientHttpResponse, HttpStatusCode, URI, HttpMethod)` will attempt to use the configured message converters to convert the response into the mapped subclass of `RestClientException`.

### setSeriesMapping

public void setSeriesMapping(Map<HttpStatus.Series,Class<? extends RestClientException>> seriesMapping)
Set the mapping from HTTP status series to `RestClientException` subclass. If this mapping has a match for the status code of a given `ClientHttpResponse`, `DefaultResponseErrorHandler.hasError(ClientHttpResponse)` will return `true` and `handleError(ClientHttpResponse, HttpStatusCode, URI, HttpMethod)` will attempt to use the configured message converters to convert the response into the mapped subclass of `RestClientException`.

### hasError

protected boolean hasError(HttpStatusCode statusCode)
Description copied from class: `DefaultResponseErrorHandler`
Template method called from `DefaultResponseErrorHandler.hasError(ClientHttpResponse)`.

The default implementation checks `HttpStatusCode.isError()`. Can be overridden in subclasses.

Overrides:
  `hasError` in class `DefaultResponseErrorHandler`
Parameters:
  `statusCode` - the HTTP status code
Returns:
  `true` if the response indicates an error; `false` otherwise
See Also:
  - `HttpStatusCode.isError()`

### 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, to `HttpServerErrorException.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 raises `UnknownHttpStatusCodeException`.

Overrides:
  `handleError` in class `DefaultResponseErrorHandler`
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)`