类 ExtractingResponseErrorHandler
- java.lang.Object
- org.springframework.web.client.DefaultResponseErrorHandler
- org.springframework.web.client.ExtractingResponseErrorHandler
- 所有已实现的接口:
ResponseErrorHandler
public class ExtractingResponseErrorHandler extends DefaultResponseErrorHandler
Implementation ofResponseErrorHandler
that usesHttpMessageConverters
to 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_ERROR
and/orHttpStatus.Series#SERVER_ERROR
tonull
.- 从以下版本开始:
- 5.0
- 作者:
- Simon Galperin, Arjen Poutsma
- 另请参阅:
RestTemplate.setErrorHandler(ResponseErrorHandler)
构造器概要
构造器 构造器 说明 ExtractingResponseErrorHandler()
Create a new, emptyExtractingResponseErrorHandler
.ExtractingResponseErrorHandler(List<HttpMessageConverter<?>> messageConverters)
Create a newExtractingResponseErrorHandler
with the givenHttpMessageConverter
instances.
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 void
handleError(ClientHttpResponse response, HttpStatus statusCode)
Handle the error based on the resolved status code.protected boolean
hasError(HttpStatus statusCode)
Template method called fromDefaultResponseErrorHandler.hasError(ClientHttpResponse)
.void
setMessageConverters(List<HttpMessageConverter<?>> messageConverters)
Set the message converters to use by this extractor.void
setSeriesMapping(Map<HttpStatus.Series,Class<? extends RestClientException>> seriesMapping)
Set the mapping from HTTP status series toRestClientException
subclass.void
setStatusMapping(Map<HttpStatus,Class<? extends RestClientException>> statusMapping)
Set the mapping from HTTP status code toRestClientException
subclass.从类继承的方法 org.springframework.web.client.DefaultResponseErrorHandler
getCharset, getHttpStatusCode, getResponseBody, handleError, hasError, hasError
从类继承的方法 java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
从接口继承的方法 org.springframework.web.client.ResponseErrorHandler
handleError
构造器详细资料
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 newExtractingResponseErrorHandler
with the givenHttpMessageConverter
instances.- 参数:
messageConverters
- the message converters to use
方法详细资料
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 toRestClientException
subclass. If this mapping has a match for the status code of a givenClientHttpResponse
,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
.
setSeriesMapping
public void setSeriesMapping(Map<HttpStatus.Series,Class<? extends RestClientException>> seriesMapping)
Set the mapping from HTTP status series toRestClientException
subclass. If this mapping has a match for the status code of a givenClientHttpResponse
,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
.
hasError
protected boolean hasError(HttpStatus statusCode)
从类复制的说明:DefaultResponseErrorHandler
Template method called fromDefaultResponseErrorHandler.hasError(ClientHttpResponse)
.The default implementation checks
HttpStatus.isError()
. Can be overridden in subclasses.- 覆盖:
hasError
在类中DefaultResponseErrorHandler
- 参数:
statusCode
- the HTTP status code as enum value- 返回:
true
if the response indicates an error;false
otherwise- 另请参阅:
HttpStatus.isError()
handleError
public void handleError(ClientHttpResponse response, HttpStatus statusCode) throws IOException
从类复制的说明:DefaultResponseErrorHandler
Handle 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
.- 覆盖:
handleError
在类中DefaultResponseErrorHandler
- 抛出:
IOException
- 另请参阅:
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)