当前位置:网站首页>糊涂工具类(hutool)post请求设置body参数为json数据
糊涂工具类(hutool)post请求设置body参数为json数据
2022-07-06 18:06:00 【穆雄雄】
大家好,我是雄雄,欢迎关注微信公众号雄雄的小课堂
今天介绍一个案例,糊涂工具类(hutool)post请求设置body参数为json数据,刚开始写的总是报错,下面是正确的代码:
/** * 添加动作 * @param projectVo * @return */
@Override
public String addProjectV3(@RequestBody ProjectVoV3 projectVo) {
JSONObject jsonObjectResult = new JSONObject();
//请求接口地址
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", "操作失败");
return jsonObjectResult.toString();
}
JSONObject obj = JSONUtil.parseObj(body);
if (obj == null) {
jsonObjectResult.putOpt("code", "500");
jsonObjectResult.putOpt("data", null);
jsonObjectResult.putOpt("msg", "操作失败");
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", "操作成功");
//标记有没有记录
//jsonObjectResult.putOpt("count", obj.getJSONArray("data").size());
return jsonObjectResult.toString();
}
} catch (Exception e) {
log.error("获取训练方案失败:", e);
e.printStackTrace();
}
return null;
}
重点是下面的这行代码:
String body = HttpUtil.createPost(url)
.contentType("application/json")
.body(JSON.toJSONString(projectVo)).execute().body();
```
边栏推荐
- JS reverse -- ob confusion and accelerated music that poked the [hornet's nest]
- Neon Optimization: an instruction optimization case of matrix transpose
- Google发布安全更新,修复Chrome中已被利用的0 day
- 数据手册中的词汇
- Yunna - work order management system and process, work order management specification
- 编译命令行终端 swift
- Yunna | work order management software, work order management software app
- Share a general compilation method of so dynamic library
- What does front-end processor mean? What is the main function? What is the difference with fortress machine?
- AI 从代码中自动生成注释文档
猜你喜欢
随机推荐
Machine learning: the difference between random gradient descent (SGD) and gradient descent (GD) and code implementation.
拖拽改变顺序
MySQL script batch queries all tables containing specified field types in the database
[advanced C language] 8 written questions of pointer
LeetCode:1175. 质数排列
736. Lisp 语法解析 : DFS 模拟题
Receive user input, height BMI, BMI detection small business entry case
盒子拉伸拉扯(左右模式)
Metauniverse urban legend 02: metaphor of the number one player
AcWing 1148. 秘密的牛奶运输 题解(最小生成树)
黑马笔记---异常处理
Gazebo的安装&与ROS的连接
AI automatically generates annotation documents from code
How to manage distributed teams?
各种语言,软件,系统的国内镜像,收藏这一个仓库就够了: Thanks-Mirror
According to the analysis of the Internet industry in 2022, how to choose a suitable position?
从零开始匹配vim(0)——vimscript 简介
Clickhouse fields are grouped and aggregated, and SQL is queried according to the granularity of any time period
Asset security issues or constraints on the development of the encryption industry, risk control + compliance has become the key to breaking the platform
Neon Optimization: an instruction optimization case of matrix transpose








