SpringBoot 項(xiàng)目使用hutool 工具進(jìn)行 http 接口調(diào)用的處理方法
寫作目的
在實(shí)際的開發(fā)過程中一個(gè)互聯(lián)網(wǎng)的項(xiàng)目來說 ,有可能會(huì)涉及到調(diào)用外部接口的實(shí)際業(yè)務(wù)場(chǎng)景,原生的比如使用httpclient 也能夠達(dá)到自己想要的結(jié)果處理 ,但是其實(shí)在實(shí)際開發(fā)的時(shí)候如果沒有使用過類似的技術(shù)處理的話或多禍?zhǔn)卓赡軙?huì)遇見問題所以這里我簡(jiǎn)單記錄一下今天使用到的工具類: hutool 進(jìn)行接口http 請(qǐng)求調(diào)用處理。
hutool簡(jiǎn)單介紹
關(guān)于hutool工具包其實(shí)本人使用的不多哈 ,這里面其實(shí)封裝處理了大量的開發(fā)日常小工具方法:
時(shí)間格式化,時(shí)間轉(zhuǎn)換,時(shí)間校驗(yàn)
http 接口調(diào)用
字符串格式化處理
國(guó)標(biāo)加密....
對(duì)于一個(gè)稍微大型的項(xiàng)目來說是一個(gè)很好用的封裝工具包('寶藏男孩'),更多的好東西需要大家去探索
實(shí)踐
這里說明一下hutool封裝了httpclient 也是能使用的但是它高度封裝了,所以我使用的是
HttpRequest
靈活性更高?。?!
引用依賴
<!-- hutool 工具包 -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.7</version>
</dependency>
<!-- 測(cè)試類-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>post
簡(jiǎn)單接口調(diào)用
@Test
public void huToolPost() {
System.out.println("--------------------------------post請(qǐng)求-----------------------------------");
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("請(qǐng)求響應(yīng)狀態(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);
}文件上傳
/**
* 文件上傳測(cè)試
*/
@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 請(qǐng)求
@Test
public void huToolGet(){
System.out.println("--------------------------------get請(qǐng)求-----------------------------------");
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("請(qǐng)求響應(yīng)狀態(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
今天拖到很晚才寫完這個(gè),幫一個(gè)同事對(duì)接一個(gè)系統(tǒng)的短信集成推送平臺(tái)剛好涉及國(guó)密3加密然后就使用hutool的http請(qǐng)求處理數(shù)據(jù)內(nèi)容了。
到此這篇關(guān)于SpringBoot 項(xiàng)目 使用hutool 工具進(jìn)行 http 接口調(diào)用的文章就介紹到這了,更多相關(guān)SpringBoot http 接口調(diào)用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Springboot重寫addInterceptors()方法配置攔截器實(shí)例
這篇文章主要介紹了Springboot重寫addInterceptors()方法配置攔截器實(shí)例,spring?boot拋棄了復(fù)雜的xml配置,我們可以自定義配置類(標(biāo)注@Configuration注解的類)來實(shí)現(xiàn)WebMvcConfigurer接口,并重寫addInterceptors()方法來配置攔截器,需要的朋友可以參考下2023-09-09
基于spring security實(shí)現(xiàn)登錄注銷功能過程解析
這篇文章主要介紹了基于spring security實(shí)現(xiàn)登錄注銷功能過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01
Java實(shí)現(xiàn)求數(shù)組最長(zhǎng)子序列算法示例
這篇文章主要介紹了Java實(shí)現(xiàn)求數(shù)組最長(zhǎng)子序列算法,涉及java針對(duì)數(shù)組的遞歸遍歷、判斷相關(guān)操作技巧,需要的朋友可以參考下2018-07-07
Java詳細(xì)分析String類與StringBuffer和StringBuilder的使用方法
當(dāng)對(duì)字符串進(jìn)行修改的時(shí)候,需要使用 StringBuffer 和 StringBuilder類,和String類不同的是,StringBuffer和 StringBuilder類的對(duì)象能夠被多次的修改,并且不產(chǎn)生新的未使用對(duì)象2022-04-04
java調(diào)用遠(yuǎn)程服務(wù)器的shell腳本以及停止的方法實(shí)現(xiàn)
這篇文章主要介紹了java調(diào)遠(yuǎn)程服務(wù)器的shell腳本以及停止的方法實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
jfinal中stateless模式嵌入shiro驗(yàn)證的實(shí)現(xiàn)方式
這篇文章主要介紹了jfinal中stateless模式嵌入shiro驗(yàn)證,今天,我們就來嘗試一種通過攔截器來實(shí)現(xiàn)的Stateless Jfinal嵌入方式,需要的朋友可以參考下2022-06-06

