Hutool 工具类

场景:

Java springboot 调用第三方接口


方法

使用hutool工具,通过HttpUtil调用post方法,方便简洁,第一次使用hutool工具,特此记录:
1、在pom.xml中添加依赖

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

2、在application.yml中添加要访问的第三方接口路径

url:
  #第三方接口
  sendUrl: http://XX.XX.XX.XX:XXX

3、在代码中调用HttpUtil的post方法

@Value("${url.sendUtl}")
private String sendUtl;

public String sendData(){
	Map<String,Object> paramsMap= new HashMap<String,Object>();
    paramsMap.put("aa","aaaa");
    paramsMap.put("bb","bbbb");
    String response = null;
    String sendMsg="";
    sendMsg= JsonUtil.toStr(paramsMap);
    try {
        response = HttpUtil.post(sendUtl+ "/xxx", sendMsg);//使用了hutool工具类 HttpUtil
    }catch (Exception e){
        e.printStackTrace();
    }
    return response;
}

总结:

HttpUtil post方法:

1、public static String post(String urlString, Map<String,Object> paramMap)
urlString - 网址
paramMap - post表单数据
2、public static String post(String urlString,String body)
urlString - 网址
body - post表单数据

 请求体body参数支持两种类型:
 1. 标准参数,例如 a=1&b=2 这种格式
 2. Rest模式,此时body需要传入一个JSON或者XML字符串,Hutool会自动绑定其对应的Content-Type

Hutool中的工具类很多,可以参考:https://www.hutool.cn/

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值