Class DefaultResponseErrorHandler
- java.lang.Object
- org.springframework.web.client.DefaultResponseErrorHandler
- All Implemented Interfaces:
ResponseErrorHandler
public class DefaultResponseErrorHandler extends Object implements ResponseErrorHandler
Spring's default implementation of theResponseErrorHandler
interface.This error handler checks for the status code on the
ClientHttpResponse
: Any code with seriesHttpStatus.Series.CLIENT_ERROR
orHttpStatus.Series.SERVER_ERROR
is considered to be an error; this behavior can be changed by overriding thehasError(HttpStatus)
method. Unknown status codes will be ignored byhasError(ClientHttpResponse)
.- Since:
- 3.0
- Author:
- Arjen Poutsma, Rossen Stoyanchev, Juergen Hoeller
- See Also:
RestTemplate.setErrorHandler(org.springframework.web.client.ResponseErrorHandler)
Constructor Summary
Constructors Constructor Description DefaultResponseErrorHandler()
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected Charset
getCharset(ClientHttpResponse response)
Determine the charset of the response (for inclusion in a status exception).protected HttpStatus
getHttpStatusCode(ClientHttpResponse response)
Determine the HTTP status of the given response.protected byte[]
getResponseBody(ClientHttpResponse response)
Read the body of the given response (for inclusion in a status exception).void
handleError(ClientHttpResponse response)
This default implementation throws aHttpClientErrorException
if the response status code isHttpStatus.Series.CLIENT_ERROR
, aHttpServerErrorException
if it isHttpStatus.Series.SERVER_ERROR
, and aRestClientException
in other cases.protected boolean
hasError(int unknownStatusCode)
Template method called fromhasError(ClientHttpResponse)
.boolean
hasError(ClientHttpResponse response)
Delegates tohasError(HttpStatus)
(for a standard status enum value) orhasError(int)
(for an unknown status code) with the response status code.protected boolean
hasError(HttpStatus statusCode)
Template method called fromhasError(ClientHttpResponse)
.
Constructor Detail
DefaultResponseErrorHandler
public DefaultResponseErrorHandler()
Method Detail
hasError
public boolean hasError(ClientHttpResponse response) throws IOException
Delegates tohasError(HttpStatus)
(for a standard status enum value) orhasError(int)
(for an unknown status code) with the response status code.- Specified by:
hasError
in interfaceResponseErrorHandler
- Parameters:
response
- the response to inspect- Returns:
true
if the response indicates an error;false
otherwise- Throws:
IOException
- in case of I/O errors- See Also:
ClientHttpResponse.getRawStatusCode()
,hasError(HttpStatus)
,hasError(int)
hasError
protected boolean hasError(HttpStatus statusCode)
Template method called fromhasError(ClientHttpResponse)
.The default implementation checks if the given status code is
CLIENT_ERROR
orSERVER_ERROR
. Can be overridden in subclasses.- Parameters:
statusCode
- the HTTP status code as enum value- Returns:
true
if the response indicates an error;false
otherwise- See Also:
HttpStatus.is4xxClientError()
,HttpStatus.is5xxServerError()
hasError
protected boolean hasError(int unknownStatusCode)
Template method called fromhasError(ClientHttpResponse)
.The default implementation checks if the given status code is
HttpStatus.Series#CLIENT_ERROR CLIENT_ERROR
orHttpStatus.Series#SERVER_ERROR SERVER_ERROR
. Can be overridden in subclasses.- Parameters:
unknownStatusCode
- the HTTP status code as raw value- Returns:
true
if the response indicates an error;false
otherwise- Since:
- 4.3.21
- See Also:
HttpStatus.Series.CLIENT_ERROR
,HttpStatus.Series.SERVER_ERROR
handleError
public void handleError(ClientHttpResponse response) throws IOException
This default implementation throws aHttpClientErrorException
if the response status code isHttpStatus.Series.CLIENT_ERROR
, aHttpServerErrorException
if it isHttpStatus.Series.SERVER_ERROR
, and aRestClientException
in other cases.- Specified by:
handleError
in interfaceResponseErrorHandler
- Parameters:
response
- the response with the error- Throws:
IOException
- in case of I/O errors
getHttpStatusCode
protected HttpStatus getHttpStatusCode(ClientHttpResponse response) throws IOException
Determine the HTTP status of the given response.Note: Only called from
handleError(org.springframework.http.client.ClientHttpResponse)
, not fromhasError(org.springframework.http.client.ClientHttpResponse)
.- Parameters:
response
- the response to inspect- Returns:
- the associated HTTP status
- Throws:
IOException
- in case of I/O errorsUnknownHttpStatusCodeException
- in case of an unknown status code that cannot be represented with theHttpStatus
enum- Since:
- 4.3.8
getResponseBody
protected byte[] getResponseBody(ClientHttpResponse response)
Read the body of the given response (for inclusion in a status exception).- Parameters:
response
- the response to inspect- Returns:
- the response body as a byte array, or an empty byte array if the body could not be read
- Since:
- 4.3.8
getCharset
protected Charset getCharset(ClientHttpResponse response)
Determine the charset of the response (for inclusion in a status exception).- Parameters:
response
- the response to inspect- Returns:
- the associated charset, or
null
if none - Since:
- 4.3.8