解決RestTemplate 請求接收自定義400+ 或500+錯(cuò)誤
RestTemplate 請求接收自定義400+ 或500+錯(cuò)誤
場景
當(dāng)服務(wù)端自定義400錯(cuò)誤返回體時(shí),使用restTemplate 請求接收不到消息體。而我正想根據(jù)不同的錯(cuò)誤信息做不同的操作。
原因
restTemplate 內(nèi)置了自己的處理異常的方法ResponseErrorHandler去處理異常
protected void handleResponse(URI url, HttpMethod method, ClientHttpResponse response) throws IOException {
ResponseErrorHandler errorHandler = getErrorHandler();
boolean hasError = errorHandler.hasError(response);
if (logger.isDebugEnabled()) {
try {
logger.debug(method.name() + " request for \"" + url + "\" resulted in " +
response.getRawStatusCode() + " (" + response.getStatusText() + ")" +
(hasError ? "; invoking error handler" : ""));
}
catch (IOException ex) {
// ignore
}
}
if (hasError) {
errorHandler.handleError(response);
}
}
當(dāng)接收到CLIENT_ERROR 或 SERVER_ERROR 時(shí),直接拋異常
@Override
public void handleError(ClientHttpResponse response) throws IOException {
HttpStatus statusCode = getHttpStatusCode(response);
switch (statusCode.series()) {
case CLIENT_ERROR:
throw new HttpClientErrorException(statusCode, response.getStatusText(),
response.getHeaders(), getResponseBody(response), getCharset(response));
case SERVER_ERROR:
throw new HttpServerErrorException(statusCode, response.getStatusText(),
response.getHeaders(), getResponseBody(response), getCharset(response));
default:
throw new RestClientException("Unknown status code [" + statusCode + "]");
}
}
解決辦法
自定義異常處理器,對(duì)響應(yīng)的錯(cuò)誤信息不進(jìn)行處理
public class FacePlusThrowErrorHandler implements ResponseErrorHandler {
@Override
public boolean hasError(ClientHttpResponse response) throws IOException {
return false;
}
@Override
public void handleError(ClientHttpResponse response) throws IOException {
}
}
之后在bean 注入時(shí),設(shè)置restTemplate 默認(rèn)異常處理器為我們自定義的
@Bean
public RestTemplate facePlusRestTemplate() {
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setConnectTimeout(300000);
requestFactory.setReadTimeout(300000);
RestTemplate restTemplate = new RestTemplate(requestFactory);
restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
restTemplate.getMessageConverters().add(new FormHttpMessageConverter());
restTemplate.setErrorHandler(new FacePlusThrowErrorHandler());
return restTemplate;
}
然后當(dāng)我們使用restTemplate 時(shí),設(shè)置restTemplate bean 名為注入時(shí)起的名字
@Resource(name = "facePlusRestTemplate") private RestTemplate restTemplate;
最后從返回的ResponseEntity 中取body 屬性,就可以取得服務(wù)端返回的消息體了
自定義RestTemplate的ResponseErrorHandler
Spring框架中的RestTemplate處理ClientHttpResponse的方式
直接看RestTemplate的源碼

這里主要判斷了是ClientError還是ServerError,即4xx或者是5xx。
如若是這兩類狀態(tài)碼,會(huì)執(zhí)行handleError()拋出異常

并不想讓它拋異常
在一些業(yè)務(wù)場景下,或許我們并不想讓它拋異常(即便我們可以捕獲異常,額外做處理),那么就需要我們ResponseErrorHandler,并且重新定義一個(gè)RestTemplate對(duì)象使用該ErrorHandler。(簡單實(shí)現(xiàn)如下)
@Configuration
public class CustomResponseErrorHandler implements ResponseErrorHandler {
@Override
public boolean hasError(ClientHttpResponse clientHttpResponse) throws IOException {
return false;
}
@Override
public void handleError(ClientHttpResponse clientHttpResponse) throws IOException {
}
@Bean("customRestTemplate")
public RestTemplate customRestTemplate() {
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setConnectTimeout(300000);
requestFactory.setReadTimeout(300000);
RestTemplate restTemplate = new RestTemplate(requestFactory);
restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
restTemplate.getMessageConverters().add(new FormHttpMessageConverter());
restTemplate.setErrorHandler(new CustomResponseErrorHandler());
return restTemplate;
}
}
還需要加入一個(gè)maven依賴(具體原因查看maven依賴圖)
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.5</version>
</dependency>
在spring 中以IOC方式注入
@Resource(name = "customRestTemplate") private RestTemplate restTemplate;
到這里已經(jīng)可以正常使用。
無法使用IOC注入的場景下
還是參照CustomResponseErrorHandler中的customRestTemplate()去new對(duì)象吧……
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用java技術(shù)抓取網(wǎng)站上彩票雙色球信息詳解
這篇文章主要介紹了使用java技術(shù)抓取網(wǎng)站上彩票雙色球信息詳解,web結(jié)果由html+js+css組成,html結(jié)構(gòu)都有一定的規(guī)范,數(shù)據(jù)動(dòng)態(tài)交互可以通過js實(shí)現(xiàn)。,需要的朋友可以參考下2019-06-06
基于springboot+vue實(shí)現(xiàn)垃圾分類管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了基于springboot+vue實(shí)現(xiàn)垃圾分類管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
Springboot詳細(xì)講解RocketMQ實(shí)現(xiàn)順序消息的發(fā)送與消費(fèi)流程
RocketMQ作為一款純java、分布式、隊(duì)列模型的開源消息中間件,支持事務(wù)消息、順序消息、批量消息、定時(shí)消息、消息回溯等,本篇我們了解如何實(shí)現(xiàn)順序消息的發(fā)送與消費(fèi)2022-06-06
SpringShell命令行之交互式Shell應(yīng)用開發(fā)方式
本文將深入探討Spring Shell的核心特性、實(shí)現(xiàn)方式及應(yīng)用場景,幫助開發(fā)者掌握這一強(qiáng)大工具,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04
SpringBoot加載應(yīng)用事件監(jiān)聽器代碼實(shí)例
這篇文章主要介紹了SpringBoot加載應(yīng)用事件監(jiān)聽器代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
Spring JDK動(dòng)態(tài)代理實(shí)現(xiàn)過程詳解
這篇文章主要介紹了Spring JDK動(dòng)態(tài)代理實(shí)現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02

