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)文章
Java異常區(qū)分和處理的一些經(jīng)驗(yàn)分享
這篇文章介紹了Java異常區(qū)分和處理的一些經(jīng)驗(yàn)分享,主要是異常選擇和使用中的一些誤區(qū)總結(jié)與歸納,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11
詳解設(shè)計(jì)模式中的proxy代理模式及在Java程序中的實(shí)現(xiàn)
代理模式主要分為靜態(tài)代理和動(dòng)態(tài)代理,使客戶端方面的使用者通過設(shè)置的代理來操作對(duì)象,下面來詳解設(shè)計(jì)模式中的proxy代理模式及在Java程序中的實(shí)現(xiàn)2016-05-05
Java如何判斷一個(gè)字符串是否包含某個(gè)字符串
這篇文章主要給大家介紹了關(guān)于Java如何判斷一個(gè)字符串是否包含某個(gè)字符串的相關(guān)資料,在實(shí)際編程中,經(jīng)常需要判斷一個(gè)字符串中是否包含某個(gè)子串,需要的朋友可以參考下2023-07-07
mybatis-plus報(bào)錯(cuò)Not Found TableInfoCache異常問題
在集成百度uid-generator過程中,MyBatis-Plus報(bào)錯(cuò)NotFoundTableInfoCache異常,解決方法:檢查實(shí)體類是否繼承了官方model,確保實(shí)體類對(duì)應(yīng)的mapper已正確注入,在使用@Component注解時(shí),應(yīng)保證相關(guān)依賴已注入2024-09-09

