当前位置:网站首页>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();
```
边栏推荐
- 黑马笔记---创建不可变集合与Stream流
- Add the applet "lazycodeloading": "requiredcomponents" in taro,
- 405 method not allowed appears when the third party jumps to the website
- Use nodejs to determine which projects are packaged + released
- 百度飞将BMN时序动作定位框架 | 数据准备与训练指南 (上)
- Dark horse notes - create immutable sets and streams
- [signal and system]
- 字符串的相关编程题
- C language - array
- Spark TPCDS Data Gen
猜你喜欢
Make Jar, Not War
Make Jar, Not War
云呐-工单管理制度及流程,工单管理规范
Installation of gazebo & connection with ROS
dvajs的基础介绍及使用
云呐|工单管理软件,工单管理软件APP
爬虫实战(六):爬笔趣阁小说
Appium automation test foundation uiautomatorviewer positioning tool
Today's question -2022/7/4 modify string reference type variables in lambda body
Appium基础 — Appium Inspector定位工具(一)
随机推荐
AcWing 1142. 繁忙的都市 题解(最小生成树)
AcWing 361. Sightseeing cow problem solution (SPFA seeking positive ring)
IDEA常用的快捷键
hdu 4661 Message Passing(木DP&组合数学)
Today's question -2022/7/4 modify string reference type variables in lambda body
安全保护能力是什么意思?等保不同级别保护能力分别是怎样?
AcWing 1148. Secret milk transportation problem solution (minimum spanning tree)
Google发布安全更新,修复Chrome中已被利用的0 day
Gazebo的安装&与ROS的连接
1123. The nearest common ancestor of the deepest leaf node
新工作感悟~辞旧迎新~
Google released a security update to fix 0 days that have been used in chrome
AcWing 344. Solution to the problem of sightseeing tour (Floyd finding the minimum ring of undirected graph)
字节P7专业级讲解:接口测试常用工具及测试方法,福利文
AcWing 345. 牛站 题解(floyd的性质、倍增)
How to prevent overfitting in cross validation
Set WordPress pseudo static connection (no pagoda)
Share a general compilation method of so dynamic library
Transformation transformation operator
分享一个通用的so动态库的编译方法