Top Description Fields Constructors Methods
org.springframework.web.client

public Class ExtractingResponseErrorHandler

extends DefaultResponseErrorHandler
Class Inheritance
Imports
java.io.IOException, java.util.Collections, .LinkedHashMap, .List, .Map, org.springframework.http.HttpStatus, .HttpStatusCode, org.springframework.http.client.ClientHttpResponse, org.springframework.http.converter.HttpMessageConverter, org.springframework.lang.Nullable, org.springframework.util.CollectionUtils

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, hasError(ClientHttpResponse) will return true, and 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.

Authors
Simon Galperin, Arjen Poutsma
Since
5.0
See Also
RestTemplate#setErrorHandler(ResponseErrorHandler)

Field Summary

Modifier and TypeField and Description
private List<HttpMessageConverter<?>>
private final Map<HttpStatus.Series, Class<? extends RestClientException>>
private final Map<HttpStatusCode, Class<? extends RestClientException>>

Constructor Summary

AccessConstructor and Description
public
ExtractingResponseErrorHandler()

Create a new, empty ExtractingResponseErrorHandler.

public
ExtractingResponseErrorHandler(List<HttpMessageConverter<?>>
the message converters to use
messageConverters
)

Create a new ExtractingResponseErrorHandler with the given HttpMessageConverter instances.

Method Summary

Modifier and TypeMethod and Description
private void
extract(Class<? extends RestClientException> exceptionClass, ClientHttpResponse response)

public void
handleError(ClientHttpResponse response, HttpStatusCode statusCode)

Overrides org.springframework.web.client.DefaultResponseErrorHandler.handleError.

Handle the error based on the resolved status code.

protected boolean
public void
setMessageConverters(List<HttpMessageConverter<?>> messageConverters)

Overrides org.springframework.web.client.DefaultResponseErrorHandler.setMessageConverters.

Set the message converters to use by this extractor.

public void
setSeriesMapping(Map<HttpStatus.Series, Class<? extends RestClientException>> seriesMapping)

Set the mapping from HTTP status series to RestClientException subclass.

public void
setStatusMapping(Map<HttpStatusCode, Class<? extends RestClientException>> statusMapping)

Set the mapping from HTTP status code to RestClientException subclass.

Inherited from org.springframework.web.client.DefaultResponseErrorHandler:
getCharsetgetResponseBodyhandleErrorhasErrorhasErrorinitBodyConvertFunction

Field Detail

messageConvertersback to summary
private List<HttpMessageConverter<?>> messageConverters

Hides org.springframework.web.client.DefaultResponseErrorHandler.messageConverters.

seriesMappingback to summary
private final Map<HttpStatus.Series, Class<? extends RestClientException>> seriesMapping
statusMappingback to summary
private final Map<HttpStatusCode, Class<? extends RestClientException>> statusMapping

Constructor Detail

ExtractingResponseErrorHandlerback to summary
public ExtractingResponseErrorHandler()

Create a new, empty ExtractingResponseErrorHandler.

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

ExtractingResponseErrorHandlerback to summary
public ExtractingResponseErrorHandler(List<HttpMessageConverter<?>> messageConverters)

Create a new ExtractingResponseErrorHandler with the given HttpMessageConverter instances.

Parameters
messageConverters:List<HttpMessageConverter<?>>

the message converters to use

Method Detail

extractback to summary
private void extract(Class<? extends RestClientException> exceptionClass, ClientHttpResponse response) throws IOException
handleErrorback to summary
public void handleError(ClientHttpResponse response, HttpStatusCode statusCode) throws IOException

Overrides org.springframework.web.client.DefaultResponseErrorHandler.handleError.

Doc from org.springframework.web.client.DefaultResponseErrorHandler.handleError.

Handle the error based on the resolved status code.

The default implementation delegates to HttpClientErrorException#create for errors in the 4xx range, to HttpServerErrorException#create for errors in the 5xx range, or otherwise raises UnknownHttpStatusCodeException.

Annotations
@Override
hasErrorback to summary
protected boolean hasError(HttpStatusCode statusCode)

Overrides org.springframework.web.client.DefaultResponseErrorHandler.hasError.

Doc from org.springframework.web.client.DefaultResponseErrorHandler.hasError.

Template method called from hasError(ClientHttpResponse).

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

Parameters
statusCode:HttpStatusCode

the HTTP status code

Returns:boolean

true if the response indicates an error; false otherwise

Annotations
@Override
setMessageConvertersback to summary
public void setMessageConverters(List<HttpMessageConverter<?>> messageConverters)

Overrides org.springframework.web.client.DefaultResponseErrorHandler.setMessageConverters.

Set the message converters to use by this extractor.

Annotations
@Override
setSeriesMappingback to summary
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, hasError(ClientHttpResponse) will return true and handleError(ClientHttpResponse) will attempt to use the configured message converters to convert the response into the mapped subclass of RestClientException.

setStatusMappingback to summary
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, hasError(ClientHttpResponse) will return true and handleError(ClientHttpResponse) will attempt to use the configured message converters to convert the response into the mapped subclass of RestClientException.