Hutool常用工具類


1.背景

  實際開發中經常用到很多的工具類,這里hutool提供了一系列的工具類,下面重點介紹常用的工具類.

2.使用步驟

  官方文檔:https://hutool.cn/docs/#/

  添加依賴

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.2.5</version>
</dependency>

3.常用工具類

 

/**
     * 字符串校驗
     */
    @Test
    public void test1() {
        //1.判定字符串是否為空
        String str = "4566";
        if (StrUtil.isEmpty(str)) {
            System.out.println("字符串為空");
        }
        //2.判定字符串是否為郵箱格式
        String email = "";
        if (Validator.isEmail(email)) {
            System.out.println("該字符串為郵件格式");
        }
        //3.判定是否為手機格式
        String phone = "";
        if (Validator.isMobile(phone)) {
            System.out.println("該字符串為手機格式");
        }
        //其他更多使用參考文檔
    }


 /**
     * httClient 請求
     */
    public void testHttClient() {
        //請求地址
        String url = "https://";
        System.out.println("請求地址:" + url);
        //定義請求方式 GET  POST  其他
        HttpRequest request = HttpUtil.createRequest(Method.GET, url);
        //請求參數
        Map<String, Object> map = new HashMap<>();
        map.put("name", "無忌");
        map.put("weixin", "851298348");
        map.put("qq", "851298348");
        //如果是form表單提交方式
        request.form(map);
        //如果是json提交方式
        //String param = JSON.toJSONString(map);
        //request.body(param);
        System.out.println("請求參數:" + map);
        //設置請求頭信息
        request.header("Authorization", "1234569");
        //其他設置....
        String respone = request.execute().body();
        System.out.println("響應結果:" + respone);
    }

4.Servlet工具-ServletUtil

//獲取nody中的參數
String paramXml = ServletUtil.getBody(request);

//獲取key value 參數
Map<String, String> map = ServletUtil.getParamMap(request);

更多請參看官方文檔

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM