Java?Hutool工具包中HttpUtil的日志統(tǒng)一打印及統(tǒng)一超時(shí)時(shí)間配置
為何要打印Http請(qǐng)求日志
使用hutool工具包中的HttpUtil,為了便于排查問題以及控制請(qǐng)求時(shí)間,每次都要在請(qǐng)求前后log日志,每次都需要設(shè)置超時(shí)時(shí)間,十分麻煩。
log.info("請(qǐng)求路徑:{},請(qǐng)求體:{}", url, body); HttpResponse response = HttpUtil.createPost(url).body(body).timeout(3000).execute(); log.info("請(qǐng)求結(jié)果:{}", response);
HttpUtil的請(qǐng)求攔截器(HttpInterceptor.Chain)、響應(yīng)攔截器(HttpInterceptor.Chain)
從HttpUtil的execute()方法點(diǎn)進(jìn)去幾步,可以看到以下代碼
/** * 請(qǐng)求前的攔截器,用于在請(qǐng)求前重新編輯請(qǐng)求 */ final HttpInterceptor.Chain<HttpRequest> requestInterceptors = GlobalInterceptor.INSTANCE.getCopiedRequestInterceptor(); /** * 響應(yīng)后的攔截器,用于在響應(yīng)后處理邏輯 */ final HttpInterceptor.Chain<HttpResponse> responseInterceptors = GlobalInterceptor.INSTANCE.getCopiedResponseInterceptor(); /** * 執(zhí)行Reuqest請(qǐng)求 * * @param isAsync 是否異步 * @return this */ public HttpResponse execute(boolean isAsync) { return doExecute(isAsync, config.requestInterceptors, config.responseInterceptors); }
這里有兩個(gè)攔截器配置,分別是請(qǐng)求攔截器配置config.requestInterceptors, 響應(yīng)攔截器配置config.responseInterceptors
繼續(xù)點(diǎn)進(jìn)去可以看到攔截器的執(zhí)行邏輯,分別在請(qǐng)求前和請(qǐng)求后執(zhí)行,代碼進(jìn)行省略,只保留我們需要的邏輯
/** * 執(zhí)行Reuqest請(qǐng)求 * * @param isAsync 是否異步 * @param requestInterceptors 請(qǐng)求攔截器列表 * @param responseInterceptors 響應(yīng)攔截器列表 * @return this */ private HttpResponse doExecute(boolean isAsync, HttpInterceptor.Chain<HttpRequest> requestInterceptors, HttpInterceptor.Chain<HttpResponse> responseInterceptors) { if (null != requestInterceptors) { for (HttpInterceptor<HttpRequest> interceptor : requestInterceptors) { interceptor.process(this); } } // 初始化URL urlWithParamIfGet(); // 初始化 connection initConnection(); // 發(fā)送請(qǐng)求 send(); // 手動(dòng)實(shí)現(xiàn)重定向 HttpResponse httpResponse = sendRedirectIfPossible(isAsync); // 獲取響應(yīng) if (null == httpResponse) { httpResponse = new HttpResponse(this.httpConnection, this.config, this.charset, isAsync, isIgnoreResponseBody()); } // 攔截響應(yīng) if (null != responseInterceptors) { for (HttpInterceptor<HttpResponse> interceptor : responseInterceptors) { interceptor.process(httpResponse); } } return httpResponse; }
HttpUtil的全局日志配置
明白了HttpUtil的execute()方法執(zhí)行過程,現(xiàn)在我們可以很方便的實(shí)現(xiàn)HttpUtil的全局日志了
項(xiàng)目啟動(dòng)時(shí)進(jìn)行添加我們自定義的攔截器即可,這里直接使用toString方法,是因?yàn)镠ttpUtil里面幫我們重寫了toString的邏輯,不需要我們自己去解析HttpRequest和HttpResponse了
@Autowired public void addRequestInterceptor() { GlobalInterceptor.INSTANCE.addRequestInterceptor(httpObj -> log.info(String.valueOf(httpObj))); GlobalInterceptor.INSTANCE.addResponseInterceptor(httpObj -> log.info(String.valueOf(httpObj))); }
HttpUtil的超時(shí)時(shí)間源碼分析
同樣的,我們點(diǎn)進(jìn)timeout()方法可以發(fā)現(xiàn),會(huì)分別設(shè)置連接超時(shí)、讀取響應(yīng)超時(shí)。
/** * 默認(rèn)連接超時(shí) */ int connectionTimeout = HttpGlobalConfig.getTimeout(); /** * 默認(rèn)讀取超時(shí) */ int readTimeout = HttpGlobalConfig.getTimeout(); /** * 設(shè)置超時(shí),單位:毫秒<br> * 超時(shí)包括: * * <pre> * 1. 連接超時(shí) * 2. 讀取響應(yīng)超時(shí) * </pre> * * @param milliseconds 超時(shí)毫秒數(shù) * @return this * @see #setConnectionTimeout(int) * @see #setReadTimeout(int) */ public HttpConfig timeout(int milliseconds) { setConnectionTimeout(milliseconds); setReadTimeout(milliseconds); return this; } /** * 設(shè)置連接超時(shí),單位:毫秒 * * @param milliseconds 超時(shí)毫秒數(shù) * @return this */ public HttpConfig setConnectionTimeout(int milliseconds) { this.connectionTimeout = milliseconds; return this; } /** * 設(shè)置連接超時(shí),單位:毫秒 * * @param milliseconds 超時(shí)毫秒數(shù) * @return this */ public HttpConfig setReadTimeout(int milliseconds) { this.readTimeout = milliseconds; return this; }
但是這兩個(gè)超時(shí)字段配置是由默認(rèn)的全局配置的。
所以配置一個(gè)全局的超時(shí)時(shí)間就比較方便了
HttpUtil的全局超時(shí)時(shí)間配置
@Autowired public void hutoolHttpUtilConfig() { // 設(shè)置hutool HttpUtil的request請(qǐng)求參數(shù)打印 GlobalInterceptor.INSTANCE.addRequestInterceptor(httpObj -> log.info(String.valueOf(httpObj))); // 設(shè)置hutool HttpUtil的response參數(shù)打印 GlobalInterceptor.INSTANCE.addResponseInterceptor(httpObj -> log.info(String.valueOf(httpObj))); // 設(shè)置hutool HttpUtil的連接超時(shí)時(shí)間、讀取響應(yīng)超時(shí)時(shí)間 HttpGlobalConfig.setTimeout(3000); }
附:hutool-http使用注意
Hutool諧音“糊涂”,一方面簡(jiǎn)潔易懂,一方面寓意“難得糊涂”,但是糊涂中可能就會(huì)掉入坑中。查看HttpUtil源碼,再調(diào)用HttpUtil中的post或者get方法后,會(huì)創(chuàng)建HttpRequest對(duì)象調(diào)用execute()方法,建立連接發(fā)送請(qǐng)求。
但是初始化連接的時(shí)候會(huì)直接創(chuàng)建一個(gè)新的連接,如果要循環(huán)調(diào)用大量接口,這個(gè)是很不合適的。
總結(jié)
到此這篇關(guān)于Java Hutool工具包中HttpUtil的日志統(tǒng)一打印及統(tǒng)一超時(shí)時(shí)間配置的文章就介紹到這了,更多相關(guān)HttpUtil日志統(tǒng)一打印內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring實(shí)戰(zhàn)之使用p:命名空間簡(jiǎn)化配置操作示例
這篇文章主要介紹了Spring實(shí)戰(zhàn)之使用p:命名空間簡(jiǎn)化配置操作,結(jié)合實(shí)例形式分析了spring p:命名空間簡(jiǎn)單配置與使用操作技巧,需要的朋友可以參考下2019-12-12關(guān)于idea中出現(xiàn)nbsp和zwsp的完美解決辦法
本文給大家介紹關(guān)于idea中出現(xiàn)nbsp和zwsp的解決辦法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2022-06-06Maven設(shè)置JDK版本的兩種方法實(shí)現(xiàn)
本文主要介紹了Maven設(shè)置JDK版本的兩種方法實(shí)現(xiàn),是通過Apache Maven Compiler Plugin插件實(shí)現(xiàn)的,具有一定的參考價(jià)值,感興趣的可以了解一下2024-07-07前端如何調(diào)用后端接口進(jìn)行數(shù)據(jù)交互詳解(axios和SpringBoot)
一般來講前端不會(huì)給后端接口,而是后端給前端接口的情況比較普遍,下面這篇文章主要給大家介紹了關(guān)于前端如何調(diào)用后端接口進(jìn)行數(shù)據(jù)交互的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-03-03Java遞歸查找層級(jí)文件夾下特定內(nèi)容的文件的方法
這篇文章主要介紹了Java遞歸查找層級(jí)文件夾下特定內(nèi)容的文件,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06SpringBoot項(xiàng)目中如何動(dòng)態(tài)切換數(shù)據(jù)源、數(shù)據(jù)庫
本文主要介紹了SpringBoot項(xiàng)目中如何動(dòng)態(tài)切換數(shù)據(jù)源、數(shù)據(jù)庫,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-02-02在Android的應(yīng)用中實(shí)現(xiàn)網(wǎng)絡(luò)圖片異步加載的方法
這篇文章主要介紹了在Android的應(yīng)用中實(shí)現(xiàn)網(wǎng)絡(luò)圖片異步加載的方法,一定程度上有助于提高安卓程序的使用體驗(yàn),需要的朋友可以參考下2015-07-07Springboot項(xiàng)目中運(yùn)用vue+ElementUI+echarts前后端交互實(shí)現(xiàn)動(dòng)態(tài)圓環(huán)圖(推薦)
今天給大家?guī)硪黄坛剃P(guān)于Springboot項(xiàng)目中運(yùn)用vue+ElementUI+echarts前后端交互實(shí)現(xiàn)動(dòng)態(tài)圓環(huán)圖的技能,包括環(huán)境配置及圓環(huán)圖前端后端實(shí)現(xiàn)代碼,感興趣的朋友一起看看吧2021-06-06Spring?Boot整合郵箱發(fā)送郵件實(shí)例
大家好,本篇文章主要講的是Spring?Boot整合郵箱發(fā)送郵件實(shí)例,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下2022-02-02