public class DefaultResponseErrorHandler extends Object implements ResponseErrorHandler
ResponseErrorHandler
interface.
This error handler checks for the status code on the ClientHttpResponse
:
Any code with series HttpStatus.Series.CLIENT_ERROR
or HttpStatus.Series.SERVER_ERROR
is considered to be
an error; this behavior can be changed by overriding the hasError(HttpStatus)
method. Unknown status codes will be ignored by hasError(ClientHttpResponse)
.
RestTemplate.setErrorHandler(org.springframework.web.client.ResponseErrorHandler)
Constructor and Description |
---|
DefaultResponseErrorHandler() |
Modifier and Type | Method and 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 a
HttpClientErrorException if the response status code
is HttpStatus.Series.CLIENT_ERROR , a HttpServerErrorException
if it is HttpStatus.Series.SERVER_ERROR ,
and a RestClientException in other cases. |
boolean |
hasError(ClientHttpResponse response)
Delegates to
hasError(HttpStatus) (for a standard status enum value) or
hasError(int) (for an unknown status code) with the response status code. |
protected boolean |
hasError(HttpStatus statusCode)
Template method called from
hasError(ClientHttpResponse) . |
protected boolean |
hasError(int unknownStatusCode)
Template method called from
hasError(ClientHttpResponse) . |
public boolean hasError(ClientHttpResponse response) throws IOException
hasError(HttpStatus)
(for a standard status enum value) or
hasError(int)
(for an unknown status code) with the response status code.hasError
in interface ResponseErrorHandler
response
- the response to inspecttrue
if the response indicates an error; false
otherwiseIOException
- in case of I/O errorsClientHttpResponse.getRawStatusCode()
,
hasError(HttpStatus)
,
hasError(int)
protected boolean hasError(HttpStatus statusCode)
hasError(ClientHttpResponse)
.
The default implementation checks if the given status code is
CLIENT_ERROR
or
SERVER_ERROR
.
Can be overridden in subclasses.
statusCode
- the HTTP status code as enum valuetrue
if the response indicates an error; false
otherwiseHttpStatus.is4xxClientError()
,
HttpStatus.is5xxServerError()
protected boolean hasError(int unknownStatusCode)
hasError(ClientHttpResponse)
.
The default implementation checks if the given status code is
HttpStatus.Series#CLIENT_ERROR CLIENT_ERROR
or
HttpStatus.Series#SERVER_ERROR SERVER_ERROR
.
Can be overridden in subclasses.
unknownStatusCode
- the HTTP status code as raw valuetrue
if the response indicates an error; false
otherwiseHttpStatus.Series#CLIENT_ERROR
,
HttpStatus.Series#SERVER_ERROR
public void handleError(ClientHttpResponse response) throws IOException
HttpClientErrorException
if the response status code
is HttpStatus.Series.CLIENT_ERROR
, a HttpServerErrorException
if it is HttpStatus.Series.SERVER_ERROR
,
and a RestClientException
in other cases.handleError
in interface ResponseErrorHandler
response
- the response with the errorIOException
- in case of I/O errorsprotected HttpStatus getHttpStatusCode(ClientHttpResponse response) throws IOException
Note: Only called from handleError(org.springframework.http.client.ClientHttpResponse)
, not from hasError(org.springframework.http.client.ClientHttpResponse)
.
response
- the response to inspectIOException
- in case of I/O errorsUnknownHttpStatusCodeException
- in case of an unknown status code
that cannot be represented with the HttpStatus
enumprotected byte[] getResponseBody(ClientHttpResponse response)
response
- the response to inspectprotected Charset getCharset(ClientHttpResponse response)
response
- the response to inspectnull
if none