当前位置:网站首页>Hutool post requests to set the body parameter to JSON data
Hutool post requests to set the body parameter to JSON data
2022-07-07 01:40:00 【Mu Xiongxiong】
Hello everyone , I'm Xiong Xiong , Welcome to WeChat official account. Xiongxiong's small class
Today, I will introduce a case , Muddle headed tool class (hutool)post The request is set body Parameter is json data , What you write at first is always wrong , Here is the correct code :
/** * Add action * @param projectVo * @return */
@Override
public String addProjectV3(@RequestBody ProjectVoV3 projectVo) {
JSONObject jsonObjectResult = new JSONObject();
// Request interface address
String url = TrainingSchemeConstant.addProjectV3;
try {
String body = HttpUtil.createPost(url)
.contentType("application/json")
.body(JSON.toJSONString(projectVo)).execute().body();
if (StringUtils.isBlank(body)) {
jsonObjectResult.putOpt("code", "500");
jsonObjectResult.putOpt("data", null);
jsonObjectResult.putOpt("msg", " operation failed ");
return jsonObjectResult.toString();
}
JSONObject obj = JSONUtil.parseObj(body);
if (obj == null) {
jsonObjectResult.putOpt("code", "500");
jsonObjectResult.putOpt("data", null);
jsonObjectResult.putOpt("msg", " operation failed ");
return jsonObjectResult.toString();
}
String code = obj.get("code").toString();
if ("200".equals(code)) {
jsonObjectResult.putOpt("code", "200");
jsonObjectResult.putOpt("data", obj.get("data"));
jsonObjectResult.putOpt("msg", " Successful operation ");
// Mark whether there is any record
//jsonObjectResult.putOpt("count", obj.getJSONArray("data").size());
return jsonObjectResult.toString();
}
} catch (Exception e) {
log.error(" Failed to get training plan :", e);
e.printStackTrace();
}
return null;
}
The key point is the following line of code :
String body = HttpUtil.createPost(url)
.contentType("application/json")
.body(JSON.toJSONString(projectVo)).execute().body();
```
边栏推荐
猜你喜欢

Appium自动化测试基础 — uiautomatorviewer定位工具

从底层结构开始学习FPGA----FIFO IP的定制与测试

405 method not allowed appears when the third party jumps to the website

2022 Google CTF SEGFAULT LABYRINTH wp

Appium automation test foundation uiautomatorviewer positioning tool

Yunna - work order management system and process, work order management specification

免费白嫖的图床对比

shell脚本快速统计项目代码行数

AcWing 345. 牛站 题解(floyd的性质、倍增)

454-百度面经1
随机推荐
JS reverse -- ob confusion and accelerated music that poked the [hornet's nest]
C语言实例_2
增加 pdf 标题浮窗
C语言实例_5
Taro applet enables wxml code compression
AcWing 1142. 繁忙的都市 题解(最小生成树)
修改px4飞控的系统时间
LeetCode. 剑指offer 62. 圆圈中最后剩下的数
ClickHouse字段分组聚合、按照任意时间段粒度查询SQL
What does security capability mean? What are the protection capabilities of different levels of ISO?
字节P7专业级讲解:接口测试常用工具及测试方法,福利文
AcWing 345. Cattle station solution (nature and multiplication of Floyd)
制作带照明的DIY焊接排烟器
[signal and system]
AcWing 904. 虫洞 题解(spfa求负环)
糊涂工具类(hutool)post请求设置body参数为json数据
[chip scheme design] pulse oximeter
爬虫实战(六):爬笔趣阁小说
永久的摇篮
编译命令行终端 swift