栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

hutool工具类的post请求不同类型参数的接口

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

hutool工具类的post请求不同类型参数的接口

hutool:5.0.5

接口测试都是这个文章的测试接口springBoot下的post接口_震旦小读者的博客-CSDN博客

一、表单提交方式(@RequestParam修饰的参数)

//        Map param = new HashMap<>();
        JSONObject param = JSONUtil.createObj();
        param.put("name", "test");
        param.put("age", 10);
        String result = HttpUtil.post(url + "/testForm", param);
        System.out.println(JSONUtil.parseObj(result));
{"msg":"操作成功","code":"200","data":{"name":"test","age":10}}

使用的参数map或者jsonObject都可以成功调用

二、@RequestBody修饰json对象参数

        JSONObject param = JSONUtil.createObj();
        param.put("name", "test");
        param.put("age", 10);
        String result = HttpUtil.post(url + "/testJsonBody", param.toString());
        System.out.println(JSONUtil.parseObj(result));
{"msg":"操作成功","code":"200","data":{"name":"test","age":10}}

参数只能使用JSONObject格式的,不能使用Map之类的,否则格式不对无法调用接口。

三、@RequestBody修饰json格式字符串参数

        JSONObject param = JSONUtil.createObj();
        param.put("name", "test");
        param.put("age", 10);
        String result = HttpUtil.post(url + "/testJsonStr", param.toString());
        System.out.println(JSONUtil.parseObj(result));
{"msg":"操作成功","code":"200","data":{"name":"test","age":10}}

参数只能使用JSONObject格式的,不能使用Map之类的,map转换为json格式的时候是用=号的,而不是:号。

四、自定义post请求

        JSONObject param = JSONUtil.createObj();
        param.put("name", "test");
        param.put("age", 10);
        HttpRequest.post(url + "/testJsonStr").header("abc", "abc").body(param.toString()).execute().body();
        String result = HttpUtil.post(url + "/testJsonStr", param.toString());
        System.out.println(JSONUtil.parseObj(result));
{"msg":"操作成功","code":"200","data":{"name":"test","age":10}}

五、更多用法可以查阅官方文档Hutool参考文档

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/876877.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号