当前位置:网站首页>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();
```
边栏推荐
猜你喜欢
【C语言进阶篇】指针的8道笔试题
How to manage distributed teams?
The difference between Tansig and logsig. Why does BP like to use Tansig
字节P7专业级讲解:接口测试常用工具及测试方法,福利文
AcWing 1148. Secret milk transportation problem solution (minimum spanning tree)
Comparison of picture beds of free white whoring
c语言—数组
永久的摇篮
C language - array
对C语言数组的再认识
随机推荐
Gin 入门实战
AcWing 361. Sightseeing cow problem solution (SPFA seeking positive ring)
搭建【Redis in CentOS7.x】
Use nodejs to determine which projects are packaged + released
AcWing 1148. Secret milk transportation problem solution (minimum spanning tree)
C语言实例_3
Set up [redis in centos7.x]
IDEA常用的快捷键
黑马笔记---异常处理
前置机是什么意思?主要作用是什么?与堡垒机有什么区别?
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
What does front-end processor mean? What is the main function? What is the difference with fortress machine?
JS es5 peut également créer des constantes?
How to manage distributed teams?
制作带照明的DIY焊接排烟器
WCF基金会
Table table setting fillet
从零开始匹配vim(0)——vimscript 简介
Appium automation test foundation uiautomatorviewer positioning tool
mongodb查看表是否导入成功