亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringBoot 項目使用hutool 工具進行 http 接口調(diào)用的處理方法

 更新時間:2022年06月03日 23:20:45   作者:Smile_X  
在實際的開發(fā)過程中一個互聯(lián)網(wǎng)的項目來說 ,有可能會涉及到調(diào)用外部接口的實際業(yè)務場景,下面通過本文給大家介紹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)文章

最新評論