SpringBoot 項目使用hutool 工具進行 http 接口調(diào)用的處理方法
寫作目的
在實際的開發(fā)過程中一個互聯(lián)網(wǎng)的項目來說 ,有可能會涉及到調(diào)用外部接口的實際業(yè)務場景,原生的比如使用httpclient 也能夠達到自己想要的結(jié)果處理 ,但是其實在實際開發(fā)的時候如果沒有使用過類似的技術(shù)處理的話或多禍首可能會遇見問題所以這里我簡單記錄一下今天使用到的工具類: hutool 進行接口http 請求調(diào)用處理。
hutool簡單介紹
關(guān)于hutool工具包其實本人使用的不多哈 ,這里面其實封裝處理了大量的開發(fā)日常小工具方法:
時間格式化,時間轉(zhuǎn)換,時間校驗
http 接口調(diào)用
字符串格式化處理
國標加密....
對于一個稍微大型的項目來說是一個很好用的封裝工具包('寶藏男孩'),更多的好東西需要大家去探索
實踐
這里說明一下hutool封裝了httpclient 也是能使用的但是它高度封裝了,所以我使用的是
HttpRequest
靈活性更高?。。?/p>
引用依賴
<!-- hutool 工具包 --> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.7.7</version> </dependency> <!-- 測試類--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency>
post
簡單接口調(diào)用
@Test public void huToolPost() { System.out.println("--------------------------------post請求-----------------------------------"); HashMap<String, String> paramMaps = new HashMap<>(4); paramMaps.put("pid", "463669875660294144"); paramMaps.put("mobile", "123456."); paramMaps.put("name", "123456."); paramMaps.put("message", ""); HttpResponse response = HttpRequest.post("http://192.168.99.202:8202/thySystem/pg-biz-sae/app/opinion/add") .header("Content-Type", "application/json") .header("token", "710515329923024896") .header("kong-request-id", "710515329923024896") .body(JSON.toJSONString(paramMaps)) .execute(); int status = response.getStatus(); System.out.println("請求響應狀態(tài)碼:" + status); String body = response.body(); System.out.println(body); JSONObject jsonObject = JSONObject.parseObject(body); Object msg = jsonObject.get("msg"); System.out.println(msg); Object code = jsonObject.get("code"); System.out.println(code); }
文件上傳
/** * 文件上傳測試 */ @Test public void huToolUploadFile(){ File f1 = new File("C:\Users\12043\Desktop\cat.jpeg"); File f2 = new File("C:\Users\12043\Desktop\cat.jpeg"); File[] files = new File[2]; files[0] = f1; files[1] = f2; HttpResponse response = HttpRequest.post("url") .form("param", "test") .form("key", files) .execute(); }
get 請求
@Test public void huToolGet(){ System.out.println("--------------------------------get請求-----------------------------------"); HashMap<String, Object> getParamMaps = new HashMap<>(5); getParamMaps.put("sortStr", "recordFlag,baseInfo.createTime"); getParamMaps.put("sortDirection", "ASC"); getParamMaps.put("filterStr", "flowAbleInfo.nodeId==craCheck"); getParamMaps.put("pageSize", 10); getParamMaps.put("pageNo", 0); HttpResponse getResponse = HttpRequest.get("http://192.168.99.202:8202/thySystem/pg-biz-sae/sae/list") .header("Content-Type", "application/json") .header("token", "710515329923024896") .header("kong-request-id", "710515329923024896").form(getParamMaps).execute(); int status1 = getResponse.getStatus(); System.out.println("請求響應狀態(tài)碼:" + status1); String body1 = getResponse.body(); System.out.println(body1); JSONObject jsonObject1 = JSONObject.parseObject(body1); Object msg1 = jsonObject1.get("msg"); System.out.println(msg1); Object code1 = jsonObject1.get("code"); System.out.println(code1); }
end
今天拖到很晚才寫完這個,幫一個同事對接一個系統(tǒng)的短信集成推送平臺剛好涉及國密3加密然后就使用hutool的http請求處理數(shù)據(jù)內(nèi)容了。
到此這篇關(guān)于SpringBoot 項目 使用hutool 工具進行 http 接口調(diào)用的文章就介紹到這了,更多相關(guān)SpringBoot http 接口調(diào)用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Springboot重寫addInterceptors()方法配置攔截器實例
這篇文章主要介紹了Springboot重寫addInterceptors()方法配置攔截器實例,spring?boot拋棄了復雜的xml配置,我們可以自定義配置類(標注@Configuration注解的類)來實現(xiàn)WebMvcConfigurer接口,并重寫addInterceptors()方法來配置攔截器,需要的朋友可以參考下2023-09-09基于spring security實現(xiàn)登錄注銷功能過程解析
這篇文章主要介紹了基于spring security實現(xiàn)登錄注銷功能過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-01-01Java詳細分析String類與StringBuffer和StringBuilder的使用方法
當對字符串進行修改的時候,需要使用 StringBuffer 和 StringBuilder類,和String類不同的是,StringBuffer和 StringBuilder類的對象能夠被多次的修改,并且不產(chǎn)生新的未使用對象2022-04-04java調(diào)用遠程服務器的shell腳本以及停止的方法實現(xiàn)
這篇文章主要介紹了java調(diào)遠程服務器的shell腳本以及停止的方法實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-03-03jfinal中stateless模式嵌入shiro驗證的實現(xiàn)方式
這篇文章主要介紹了jfinal中stateless模式嵌入shiro驗證,今天,我們就來嘗試一種通過攔截器來實現(xiàn)的Stateless Jfinal嵌入方式,需要的朋友可以參考下2022-06-06