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 ofResponseErrorHandlerthat usesHttpMessageConvertersto convert HTTP error responses toRestClientExceptions.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 returntrue, andDefaultResponseErrorHandler.handleError(ClientHttpResponse)will attempt to use the configured message converters to convert the response into the mapped subclass ofRestClientException. 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 fromHttpStatus.Series#CLIENT_ERRORand/orHttpStatus.Series#SERVER_ERRORtonull.- Since:
- 5.0
- Author:
- Simon Galperin, Arjen Poutsma
- See Also:
RestTemplate.setErrorHandler(ResponseErrorHandler)
Constructor Summary
Constructors Constructor Description ExtractingResponseErrorHandler()Create a new, emptyExtractingResponseErrorHandler.ExtractingResponseErrorHandler(List<HttpMessageConverter<?>> messageConverters)Create a newExtractingResponseErrorHandlerwith the givenHttpMessageConverterinstances.
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidhandleError(ClientHttpResponse response, HttpStatus statusCode)Handle the error based on the resolved status code.protected booleanhasError(HttpStatus statusCode)Template method called fromDefaultResponseErrorHandler.hasError(ClientHttpResponse).voidsetMessageConverters(List<HttpMessageConverter<?>> messageConverters)Set the message converters to use by this extractor.voidsetSeriesMapping(Map<HttpStatus.Series,Class<? extends RestClientException>> seriesMapping)Set the mapping from HTTP status series toRestClientExceptionsubclass.voidsetStatusMapping(Map<HttpStatus,Class<? extends RestClientException>> statusMapping)Set the mapping from HTTP status code toRestClientExceptionsubclass.Methods inherited from class org.springframework.web.client.DefaultResponseErrorHandler
getCharset, getHttpStatusCode, getResponseBody, handleError, hasError, hasError
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.springframework.web.client.ResponseErrorHandler
handleError
Constructor Detail
ExtractingResponseErrorHandler
public ExtractingResponseErrorHandler()
Create a new, emptyExtractingResponseErrorHandler.Note that
setMessageConverters(List)must be called when using this constructor.
ExtractingResponseErrorHandler
public ExtractingResponseErrorHandler(List<HttpMessageConverter<?>> messageConverters)
Create a newExtractingResponseErrorHandlerwith the givenHttpMessageConverterinstances.- Parameters:
messageConverters- the message converters to use
Method Detail
setMessageConverters
public void setMessageConverters(List<HttpMessageConverter<?>> messageConverters)
Set the message converters to use by this extractor.
setStatusMapping
public void setStatusMapping(Map<HttpStatus,Class<? extends RestClientException>> statusMapping)
Set the mapping from HTTP status code toRestClientExceptionsubclass. If this mapping has a match for the status code of a givenClientHttpResponse,DefaultResponseErrorHandler.hasError(ClientHttpResponse)will returntrueandDefaultResponseErrorHandler.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 toRestClientExceptionsubclass. If this mapping has a match for the status code of a givenClientHttpResponse,DefaultResponseErrorHandler.hasError(ClientHttpResponse)will returntrueandDefaultResponseErrorHandler.handleError(ClientHttpResponse)will attempt to use the configured message converters to convert the response into the mapped subclass ofRestClientException.
hasError
protected boolean hasError(HttpStatus statusCode)
Description copied from class:DefaultResponseErrorHandlerTemplate method called fromDefaultResponseErrorHandler.hasError(ClientHttpResponse).The default implementation checks
HttpStatus.isError(). Can be overridden in subclasses.- Overrides:
hasErrorin classDefaultResponseErrorHandler- Parameters:
statusCode- the HTTP status code as enum value- Returns:
trueif the response indicates an error;falseotherwise- See Also:
HttpStatus.isError()
handleError
public void handleError(ClientHttpResponse response, HttpStatus statusCode) throws IOException
Description copied from class:DefaultResponseErrorHandlerHandle the error based on the resolved status code.The default implementation delegates to
HttpClientErrorException.create(org.springframework.http.HttpStatus, java.lang.String, org.springframework.http.HttpHeaders, byte[], java.nio.charset.Charset)for errors in the 4xx range, toHttpServerErrorException.create(org.springframework.http.HttpStatus, java.lang.String, org.springframework.http.HttpHeaders, byte[], java.nio.charset.Charset)for errors in the 5xx range, or otherwise raisesUnknownHttpStatusCodeException.- Overrides:
handleErrorin classDefaultResponseErrorHandler- Throws:
IOException- See Also:
HttpClientErrorException.create(org.springframework.http.HttpStatus, java.lang.String, org.springframework.http.HttpHeaders, byte[], java.nio.charset.Charset),HttpServerErrorException.create(org.springframework.http.HttpStatus, java.lang.String, org.springframework.http.HttpHeaders, byte[], java.nio.charset.Charset)