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);

更多请参看官方文档

猜你喜欢

转载自www.cnblogs.com/zhetiankj/p/12732223.html