SpringBoot自定義RestTemplate的攔截器鏈的實戰(zhàn)指南
引言
在項目開發(fā)中,RestTemplate作為Spring提供的HTTP客戶端工具,經(jīng)常用于訪問內(nèi)部或三方服務(wù)。
但在實際項目中,我們往往需要對請求進(jìn)行統(tǒng)一處理:比如添加認(rèn)證Token、記錄請求日志、設(shè)置超時時間、實現(xiàn)失敗重試等。
如果每個接口調(diào)用都手動處理這些邏輯,不僅代碼冗余,還容易出錯。
一、為什么需要攔截器鏈?
先看一個場景:假設(shè)我們的支付服務(wù)需要調(diào)用第三方支付接口,每次請求都要做這些事
- 添加
Authorization
請求頭(認(rèn)證Token) - 記錄請求URL、參數(shù)、耗時(日志審計)
- 設(shè)置3秒超時時間(防止阻塞)
- 失敗時重試2次(網(wǎng)絡(luò)抖動處理)
如果沒有攔截器,代碼會寫成這樣:
// 調(diào)用第三方支付接口 public String createOrder(String orderId) { // 1. 創(chuàng)建RestTemplate(重復(fù)10次) RestTemplate restTemplate = new RestTemplate(); // 2. 設(shè)置超時(重復(fù)10次) SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory(); factory.setConnectTimeout(3000); factory.setReadTimeout(3000); restTemplate.setRequestFactory(factory); // 3. 添加認(rèn)證頭(重復(fù)10次) HttpHeaders headers = new HttpHeaders(); headers.set("Authorization", "Bearer " + getToken()); HttpEntity<String> entity = new HttpEntity<>(headers); // 4. 記錄日志(重復(fù)10次) log.info("請求支付接口: {}", orderId); try { String result = restTemplate.postForObject(PAY_URL, entity, String.class); log.info("請求成功: {}", result); return result; } catch (Exception e) { log.error("請求失敗", e); // 5. 重試(重復(fù)10次) for (int i = 0; i < 2; i++) { try { return restTemplate.postForObject(PAY_URL, entity, String.class); } catch (Exception ex) { /* 重試邏輯 */ } } throw e; } }
這段代碼的問題很明顯:重復(fù)邏輯散落在各個接口調(diào)用中,維護(hù)成本極高。如果有10個接口需要調(diào)用第三方服務(wù),就要寫10遍相同的代碼。
而攔截器鏈能把這些通用邏輯抽離成獨立組件,按順序串聯(lián)執(zhí)行,實現(xiàn)“一次定義,處處復(fù)用”。
// 重構(gòu)后:一行搞定所有通用邏輯 return restTemplate.postForObject(PAY_URL, entity, String.class);
二、RestTemplate攔截器基礎(chǔ)
2.1 核心接口:ClientHttpRequestInterceptor
Spring提供了ClientHttpRequestInterceptor
接口,所有自定義攔截器都要實現(xiàn)它
public interface ClientHttpRequestInterceptor { // request:請求對象(可修改請求頭、參數(shù)) // body:請求體字節(jié)數(shù)組 // execution:執(zhí)行器(用于調(diào)用下一個攔截器) ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException; }
攔截器工作流程:
- 攔截器可以修改請求(如添加頭信息、修改參數(shù))
- 通過
execution.execute(request, body)
調(diào)用下一個攔截器 - 最終執(zhí)行器會發(fā)送實際HTTP請求,返回響應(yīng)
- 攔截器可以處理響應(yīng)(如記錄日志、解析響應(yīng)體)
2.2 第一個攔截器:日志攔截器
先實現(xiàn)一個打印請求響應(yīng)日志的攔截器,這是項目中最常用的功能:
import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpRequest; import org.springframework.http.client.ClientHttpRequestExecution; import org.springframework.http.client.ClientHttpRequestInterceptor; import org.springframework.http.client.ClientHttpResponse; import org.springframework.util.StreamUtils; import java.io.IOException; import java.nio.charset.StandardCharsets; @Slf4j public class LoggingInterceptor implements ClientHttpRequestInterceptor { @Override public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { // 1. 記錄請求信息 long start = System.currentTimeMillis(); log.info("===== 請求開始 ====="); log.info("URL: {}", request.getURI()); log.info("Method: {}", request.getMethod()); log.info("Headers: {}", request.getHeaders()); log.info("Body: {}", new String(body, StandardCharsets.UTF_8)); // 2. 執(zhí)行下一個攔截器(關(guān)鍵!不調(diào)用會中斷鏈條) ClientHttpResponse response = execution.execute(request, body); // 3. 記錄響應(yīng)信息 String responseBody = StreamUtils.copyToString(response.getBody(), StandardCharsets.UTF_8); log.info("===== 請求結(jié)束 ====="); log.info("Status: {}", response.getStatusCode()); log.info("Headers: {}", response.getHeaders()); log.info("Body: {}", responseBody); log.info("耗時: {}ms", System.currentTimeMillis() - start); return response; } }
關(guān)鍵點:
execution.execute(...)
是調(diào)用鏈的核心,必須調(diào)用才能繼續(xù)執(zhí)行- 響應(yīng)流
response.getBody()
只能讀取一次,如需多次處理需緩存(后面會講解決方案)
三、攔截器鏈實戰(zhàn):從單攔截器到多攔截器協(xié)同
3.1 注冊攔截器鏈
SpringBoot中通過RestTemplate.setInterceptors()
注冊多個攔截器,形成調(diào)用鏈:
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.client.BufferingClientHttpRequestFactory; import org.springframework.http.client.ClientHttpRequestInterceptor; import org.springframework.http.client.SimpleClientHttpRequestFactory; import org.springframework.web.client.RestTemplate; import java.util.ArrayList; import java.util.List; @Configuration public class RestTemplateConfig { @Bean public RestTemplate customRestTemplate() { // 1. 創(chuàng)建攔截器列表(順序很重要?。? List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>(); interceptors.add(new AuthInterceptor()); // 1. 認(rèn)證攔截器(先加請求頭) interceptors.add(new LoggingInterceptor()); // 2. 日志攔截器(記錄完整請求) interceptors.add(new RetryInterceptor(2)); // 3. 重試攔截器(最后處理失?。? // 2. 創(chuàng)建RestTemplate并設(shè)置攔截器 RestTemplate restTemplate = new RestTemplate(); restTemplate.setInterceptors(interceptors); // 3. 解決響應(yīng)流只能讀取一次的問題(關(guān)鍵配置?。? SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory(); factory.setConnectTimeout(3000); // 連接超時3秒 factory.setReadTimeout(3000); // 讀取超時3秒 restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(factory)); return restTemplate; } }
攔截器執(zhí)行順序:按添加順序執(zhí)行(像排隊一樣),上述示例的執(zhí)行流程是:
AuthInterceptor → LoggingInterceptor → RetryInterceptor → 實際請求 → RetryInterceptor → LoggingInterceptor → AuthInterceptor
3.2 核心攔截器實現(xiàn)
(1)認(rèn)證攔截器:自動添加Token
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpRequest; import org.springframework.http.client.ClientHttpRequestExecution; import org.springframework.http.client.ClientHttpRequestInterceptor; import org.springframework.http.client.ClientHttpResponse; import java.io.IOException; public class AuthInterceptor implements ClientHttpRequestInterceptor { @Override public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { // 1. 從緩存獲取Token(實際項目可能從Redis或配置中心獲?。? String token = getToken(); // 2. 添加Authorization請求頭 HttpHeaders headers = request.getHeaders(); headers.set("Authorization", "Bearer " + token); // Bearer認(rèn)證格式 // 3. 傳遞給下一個攔截器 return execution.execute(request, body); } // 模擬從緩存獲取Token private String getToken() { return "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."; // 實際項目是真實Token } }
(2)重試攔截器:失敗自動重試
package com.example.rest; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpRequest; import org.springframework.http.client.ClientHttpRequestExecution; import org.springframework.http.client.ClientHttpRequestInterceptor; import org.springframework.http.client.ClientHttpResponse; import org.springframework.web.client.HttpStatusCodeException; import java.io.IOException; @Slf4j public class RetryInterceptor implements ClientHttpRequestInterceptor { private final int maxRetries; // 最大重試次數(shù) public RetryInterceptor(int maxRetries) { this.maxRetries = maxRetries; } @Override public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { int retryCount = 0; while (true) { try { // 執(zhí)行請求(調(diào)用下一個攔截器或?qū)嶋H請求) return execution.execute(request, body); } catch (HttpStatusCodeException e) { // 只對5xx服務(wù)器錯誤重試(業(yè)務(wù)錯誤不重試) if (e.getStatusCode().is5xxServerError() && retryCount < maxRetries) { retryCount++; log.warn("服務(wù)器錯誤,開始第{}次重試,狀態(tài)碼:{}", retryCount, e.getStatusCode()); continue; } throw e; // 非5xx錯誤或達(dá)到最大重試次數(shù) } catch (IOException e) { // 網(wǎng)絡(luò)異常重試(如連接超時、DNS解析失?。? if (retryCount < maxRetries) { retryCount++; log.warn("網(wǎng)絡(luò)異常,開始第{}次重試", retryCount, e); continue; } throw e; } } } }
四、實戰(zhàn)踩坑指南
4.1 響應(yīng)流只能讀取一次問題
現(xiàn)象:日志攔截器讀取響應(yīng)體后,后續(xù)攔截器再讀會讀取到空數(shù)據(jù)。
原因:響應(yīng)流默認(rèn)是一次性的,讀完就關(guān)閉了。
解決方案:用BufferingClientHttpRequestFactory
包裝請求工廠,緩存響應(yīng)流:
// 創(chuàng)建支持響應(yīng)緩存的工廠(已在3.1節(jié)配置中體現(xiàn)) SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory(); BufferingClientHttpRequestFactory bufferingFactory = new BufferingClientHttpRequestFactory(factory); restTemplate.setRequestFactory(bufferingFactory);
4.2 攔截器順序不當(dāng)導(dǎo)致功能異常
反例:把LoggingInterceptor
放在AuthInterceptor
前面,日志中會看不到Authorization
頭,因為日志攔截器先執(zhí)行時還沒添加認(rèn)證頭。
正確順序(按職責(zé)劃分):
- 1. 前置處理攔截器:認(rèn)證、加密、參數(shù)修改
- 2. 日志攔截器:記錄完整請求
- 3. 重試/降級攔截器:處理異常情況
4.3 線程安全問題
RestTemplate
是線程安全的,但攔截器若有成員變量,需確保線程安全!
錯誤示例
public class BadInterceptor implements ClientHttpRequestInterceptor { private int count = 0; // ? 多線程共享變量,會導(dǎo)致計數(shù)錯誤 @Override public ClientHttpResponse intercept(...) { count++; // 危險!多線程下count值不準(zhǔn) // ... } }
解決方案
- 攔截器避免定義可變成員變量
- 必須使用時,用
ThreadLocal
隔離線程狀態(tài)
五、測試示例
5.1 測試接口
package com.example.rest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController public class TestController { @Autowired private RestTemplate customRestTemplate; // 注入自定義的RestTemplate @GetMapping("/call-api") public String callThirdApi() { // 調(diào)用第三方API,攔截器自動處理認(rèn)證、日志、重試 return customRestTemplate.getForObject("http://localhost:8080/mock-third-api", String.class); } // 模擬一個三方接口 @GetMapping("/mock-third-api") public String mockThirdApi() { return "hello"; } }
5.2 依賴與配置
只需基礎(chǔ)的Spring Boot Starter Web:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
六、總結(jié)與擴(kuò)展
通過自定義RestTemplate的攔截器鏈,我們可以將請求處理的通用邏輯(認(rèn)證、日志、重試等)抽離成獨立組件,實現(xiàn)代碼復(fù)用和統(tǒng)一維護(hù)。
核心要點
- 1. 攔截器鏈順序:按“前置處理→日志→重試”順序注冊,確保功能正確
- 2. 響應(yīng)流處理:使用
BufferingClientHttpRequestFactory
解決流只能讀取一次問題 - 3. 線程安全:攔截器避免定義可變成員變量,必要時使用
ThreadLocal
- 4. 異常處理:重試攔截器需明確重試條件(如只對5xx錯誤重試)
以上就是SpringBoot自定義RestTemplate的攔截器鏈的實戰(zhàn)指南的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot RestTemplate攔截器鏈的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
解決mac最新版intellij idea崩潰閃退crash的問題
這篇文章主要介紹了解決mac最新版intellij idea崩潰閃退crash的問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09Spring Boot攔截器Interceptor與過濾器Filter深度解析(區(qū)別、實現(xiàn)與實戰(zhàn)指南)
這篇文章主要介紹了Spring Boot攔截器Interceptor與過濾器Filter深度解析(區(qū)別、實現(xiàn)與實戰(zhàn)指南),本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2025-05-05自定義Jackson的ObjectMapper如何實現(xiàn)@ResponseBody的自定義渲染
這篇文章主要介紹了自定義Jackson的ObjectMapper如何實現(xiàn)@ResponseBody的自定義渲染,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07