当前位置:网站首页>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();
```
边栏推荐
猜你喜欢
Start from the bottom structure to learn the customization and testing of fpga---- FIFO IP
[signal and system]
MySQL script batch queries all tables containing specified field types in the database
2022 Google CTF SEGFAULT LABYRINTH wp
Yunna | work order management software, work order management software app
对C语言数组的再认识
Js逆向——捅了【马蜂窝】的ob混淆与加速乐
2022 Google CTF segfault Labyrinth WP
Typical problems of subnet division and super network construction
从底层结构开始学习FPGA----FIFO IP的定制与测试
随机推荐
前置机是什么意思?主要作用是什么?与堡垒机有什么区别?
拖拽改变顺序
Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
2022 Google CTF segfault Labyrinth WP
Yunna | work order management measures, how to carry out work order management
安全保护能力是什么意思?等保不同级别保护能力分别是怎样?
C language instance_ three
【C语言进阶篇】指针的8道笔试题
736. Lisp 语法解析 : DFS 模拟题
交叉验证如何防止过拟合
Neon Optimization: summary of performance optimization experience
curl 命令
AcWing 1140. Shortest network (minimum spanning tree)
使用nodejs完成判断哪些项目打包+发版
AI 从代码中自动生成注释文档
Make Jar, Not War
AcWing 346. Solution to the problem of water splashing festival in the corridor (deduction formula, minimum spanning tree)
【芯片方案设计】脉搏血氧仪
字节P7专业级讲解:接口测试常用工具及测试方法,福利文
Typical problems of subnet division and super network construction