欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot自定義RestTemplate的攔截器鏈的實戰(zhàn)指南

 更新時間:2025年07月15日 08:25:36   作者:風(fēng)象南  
在項目開發(fā)中,RestTemplate作為Spring提供的HTTP客戶端工具,經(jīng)常用于訪問內(nèi)部或三方服務(wù),但在實際項目中,我們往往需要對請求進(jìn)行統(tǒng)一處理,所以本文給大家介紹了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)用第三方支付接口,每次請求都要做這些事

  1. 添加Authorization請求頭(認(rèn)證Token)
  2. 記錄請求URL、參數(shù)、耗時(日志審計)
  3. 設(shè)置3秒超時時間(防止阻塞)
  4. 失敗時重試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;
}

攔截器工作流程

  1. 攔截器可以修改請求(如添加頭信息、修改參數(shù))
  2. 通過execution.execute(request, body)調(diào)用下一個攔截器
  3. 最終執(zhí)行器會發(fā)送實際HTTP請求,返回響應(yīng)
  4. 攔截器可以處理響應(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)文章

  • SpringBoot對接小程序微信支付的實現(xiàn)

    SpringBoot對接小程序微信支付的實現(xiàn)

    本文主要介紹了SpringBoot對接小程序微信支付的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧<BR>
    2023-09-09
  • 實現(xiàn)Java刪除一個集合的多個元素

    實現(xiàn)Java刪除一個集合的多個元素

    Java中的For each實際上使用的是iterator進(jìn)行處理的。而iterator是不允許集合在iterator使用期間刪除的。而我在for each時,從集合中刪除了一個元素,這導(dǎo)致了iterator拋出了ConcurrentModificationException,下面來看看到底怎么回事。
    2016-08-08
  • SpringBoot中如何打印Http請求日志

    SpringBoot中如何打印Http請求日志

    所有針對第三方的請求都強(qiáng)烈推薦打印請求日志,本文主要介紹了SpringBoot中如何打印Http請求日志,具有一定的參考價值,感興趣的可以了解一下
    2024-06-06
  • Java Servlet上傳圖片到指定文件夾并顯示圖片

    Java Servlet上傳圖片到指定文件夾并顯示圖片

    在學(xué)習(xí)Servlet過程中,針對圖片上傳做了一個Demo,如果大家對Java Servlet上傳圖片到指定文件夾并顯示圖片功能感興趣的朋友大家通過本文一起學(xué)習(xí)吧
    2017-08-08
  • 解決mac最新版intellij idea崩潰閃退crash的問題

    解決mac最新版intellij idea崩潰閃退crash的問題

    這篇文章主要介紹了解決mac最新版intellij idea崩潰閃退crash的問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-09-09
  • Spring Boot攔截器Interceptor與過濾器Filter深度解析(區(qū)別、實現(xiàn)與實戰(zhàn)指南)

    Spring Boot攔截器Interceptor與過濾器Filter深度解析(區(qū)別、實現(xiàn)與實戰(zhàn)指南)

    這篇文章主要介紹了Spring Boot攔截器Interceptor與過濾器Filter深度解析(區(qū)別、實現(xiàn)與實戰(zhàn)指南),本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2025-05-05
  • SpringBoot?分模塊開發(fā)的操作方法

    SpringBoot?分模塊開發(fā)的操作方法

    這篇文章主要介紹了SpringBoot?分模塊開發(fā)的操作方法,通過在原項目新增一個maven模塊,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • 自定義Jackson的ObjectMapper如何實現(xiàn)@ResponseBody的自定義渲染

    自定義Jackson的ObjectMapper如何實現(xiàn)@ResponseBody的自定義渲染

    這篇文章主要介紹了自定義Jackson的ObjectMapper如何實現(xiàn)@ResponseBody的自定義渲染,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • 詳解jvm對象的創(chuàng)建和分配

    詳解jvm對象的創(chuàng)建和分配

    這篇文章主要介紹了jvm對象的創(chuàng)建和分配的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下
    2021-03-03
  • FeignClient中name和url屬性的作用說明

    FeignClient中name和url屬性的作用說明

    這篇文章主要介紹了FeignClient中name和url屬性的作用說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06

最新評論