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

public Class DefaultResponseErrorHandler

extends Object
implements ResponseErrorHandler
Class Inheritance
All Implemented Interfaces
org.springframework.web.client.ResponseErrorHandler
Known Direct Subclasses
org.springframework.web.client.ExtractingResponseErrorHandler
Imports
java.io.ByteArrayInputStream, .IOException, .InputStream, java.nio.charset.Charset, .StandardCharsets, java.util.Collections, .List, java.util.function.Function, org.springframework.core.ResolvableType, org.springframework.core.log.LogFormatUtils, org.springframework.http.HttpHeaders, .HttpStatus, .HttpStatusCode, .MediaType, org.springframework.http.client.ClientHttpResponse, org.springframework.http.converter.HttpMessageConverter, org.springframework.lang.Nullable, org.springframework.util.Assert, .CollectionUtils, .FileCopyUtils, .ObjectUtils

Spring's default implementation of the ResponseErrorHandler interface.

This error handler checks for the status code on the ClientHttpResponse. Any code in the 4xx or 5xx series is considered to be an error. This behavior can be changed by overriding hasError(HttpStatusCode). Unknown status codes will be ignored by hasError(ClientHttpResponse).

See handleError(ClientHttpResponse) for more details on specific exception types.

Authors
Arjen Poutsma, Rossen Stoyanchev, Juergen Hoeller
Since
3.0
See Also
RestTemplate#setErrorHandler

Field Summary

Modifier and TypeField and Description
private List<HttpMessageConverter<?>>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected Charset

Returns:

the associated charset, or null if none
getCharset
(ClientHttpResponse
the response to inspect
response
)

Determine the charset of the response (for inclusion in a status exception).

private String
getErrorMessage(int rawStatusCode, String statusText, byte[] responseBody, Charset charset)

Return error message with details from the response body.

protected byte[]

Returns:

the response body as a byte array, or an empty byte array if the body could not be read
getResponseBody
(ClientHttpResponse
the response to inspect
response
)

Read the body of the given response (for inclusion in a status exception).

public void
handleError(ClientHttpResponse
the response with the error
response
)

Implements org.springframework.web.client.ResponseErrorHandler.handleError.

Handle the error in the given response with the given resolved status code.

protected void
handleError(ClientHttpResponse response, HttpStatusCode statusCode)

Handle the error based on the resolved status code.

public boolean
hasError(ClientHttpResponse
the response to inspect
response
)

Implements org.springframework.web.client.ResponseErrorHandler.hasError.

Delegates to hasError(HttpStatusCode) with the response status code.

protected boolean

Returns:

true if the response indicates an error; false otherwise
hasError
(HttpStatusCode
the HTTP status code
statusCode
)

Template method called from hasError(ClientHttpResponse).

protected boolean

Returns:

true if the response indicates an error; false otherwise
hasError
(int
the HTTP status code as raw value
statusCode
)
Deprecated in favor of hasError(HttpStatusCode)

Template method called from hasError(ClientHttpResponse).

protected Function<ResolvableType, ?>
initBodyConvertFunction(ClientHttpResponse response, byte[] body)

Return a function for decoding the error content.

pack-priv void
setMessageConverters(List<HttpMessageConverter<?>> converters)

For internal use from the RestTemplate, to pass the message converters to use to decode error content.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

messageConvertersback to summary
private List<HttpMessageConverter<?>> messageConverters
Annotations
@Nullable

Constructor Detail

DefaultResponseErrorHandlerback to summary
public DefaultResponseErrorHandler()

Method Detail

getCharsetback to summary
protected Charset getCharset(ClientHttpResponse response)

Determine the charset of the response (for inclusion in a status exception).

Parameters
response:ClientHttpResponse

the response to inspect

Returns:Charset

the associated charset, or null if none

Annotations
@Nullable
Since
4.3.8
getErrorMessageback to summary
private String getErrorMessage(int rawStatusCode, String statusText, byte[] responseBody, Charset charset)

Return error message with details from the response body. For example:

404 Not Found: [{'id': 123, 'message': 'my message'}]
getResponseBodyback to summary
protected byte[] getResponseBody(ClientHttpResponse response)

Read the body of the given response (for inclusion in a status exception).

Parameters
response:ClientHttpResponse

the response to inspect

Returns:byte[]

the response body as a byte array, or an empty byte array if the body could not be read

Since
4.3.8
handleErrorback to summary
public void handleError(ClientHttpResponse response) throws IOException

Implements org.springframework.web.client.ResponseErrorHandler.handleError.

Handle the error in the given response with the given resolved status code.

The default implementation throws:

Parameters
response:ClientHttpResponse

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

the response with the error

Annotations
@Override
Exceptions
IOException:

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

in case of I/O errors

UnknownHttpStatusCodeException:
in case of an unresolvable status code
See Also
handleError(ClientHttpResponse, HttpStatusCode)
handleErrorback to summary
protected void handleError(ClientHttpResponse response, HttpStatusCode statusCode) throws IOException

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.

Since
5.0
See Also
HttpClientErrorException#create, HttpServerErrorException#create
hasErrorback to summary
public boolean hasError(ClientHttpResponse response) throws IOException

Implements org.springframework.web.client.ResponseErrorHandler.hasError.

Delegates to hasError(HttpStatusCode) with the response status code.

Parameters
response:ClientHttpResponse

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

the response to inspect

Returns:boolean

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

true if the response indicates an error; false otherwise

Annotations
@Override
Exceptions
IOException:

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

in case of I/O errors

See Also
ClientHttpResponse#getStatusCode(), hasError(HttpStatusCode)
hasErrorback to summary
protected boolean hasError(HttpStatusCode statusCode)

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

See Also
HttpStatusCode#isError()
hasErrorback to summary
protected boolean hasError(int statusCode)

Deprecated

in favor of hasError(HttpStatusCode)

Template method called from hasError(ClientHttpResponse).

The default implementation checks if the given status code is CLIENT_ERROR or SERVER_ERROR. Can be overridden in subclasses.

Parameters
statusCode:int

the HTTP status code as raw value

Returns:boolean

true if the response indicates an error; false otherwise

Annotations
@Deprecated
Since
4.3.21
See Also
org.springframework.http.HttpStatus.Series#CLIENT_ERROR, org.springframework.http.HttpStatus.Series#SERVER_ERROR
initBodyConvertFunctionback to summary
protected Function<ResolvableType, ?> initBodyConvertFunction(ClientHttpResponse response, byte[] body)

Return a function for decoding the error content. This can be passed to RestClientResponseException#setBodyConvertFunction(Function).

Since
6.0
setMessageConvertersback to summary
pack-priv void setMessageConverters(List<HttpMessageConverter<?>> converters)

For internal use from the RestTemplate, to pass the message converters to use to decode error content.

Since
6.0