如何利用一网通办app助力企业数字化转型的成功之路
710
2023-07-07
Spring boot @RequestBody数据传递过程详解
这篇文章主要介绍了Spring boot @RequestBody数据传递过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
@RequestBody需要接的参数是一个string化的json
@RequestBody,要读取的数据在请求体里,所以要发post请求,还要将Content-Type设置为application/json
java的api
参数为JSONObject,获取到的参数处理
@PostMapping("/combine")
public Result combine(@RequestBody JSONObject jsonParam) {
System.out.println(jsonParam.toJSONString());
System.out.println(jsonParam.get("fileName"));
ArrayList ll= (ArrayList)jsonParam.get("ops");
for(int i = 0;i < ll.size(); i ++){
ArrayList mm = (ArrayList)ll.get(i);
for(int j = 0;j < mm.size(); j ++){
System.out.println(mm.get(j));
}
}
return ResultGenerator.genSuccessResult();
}
(1)js调用
var dispatchesDTO = {"fileName":"E:/java/project/ppt/data/tmpTest.pptx","ops":[["E:/java/project/ppt/data/test1.pptx","1,2,9"],["E:/java/project/ppt/data/Threejs动画.pptx","1"],["E:/java/project/ppt/data/系统时间和实时天气.pptx","1"]]};
$.ajax({
type: "post",
contentType:"application/json",
url: "http://localhost:8080/api/ppt/combine",
data: JSON.stringify(dispatchesDTO),
success: function(data){
console.log(data);
}
});
(2)php的curl调用
//初始化
$curl = curl_init();
//设置抓取的url
curl_setopt($curl, CURLOPT_URL, 'http://localhost:8080/api/ppt/combine');
$aa = [
"fileName" => "E:/java/project/ppt/data/tmp.pptx",
"ops" => [['E:/java/project/ppt/data/test1.pptx', '1,2,9'],
['E:/java/project/ppt/data/test1.pptx', '2'],
],
];
$data_string = json_encode($aa);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',http://
'Content-Length: ' . strlen($data_string),
));
//执行命令
$data = curl_exec($curl);
//关闭URL请求
curl_close($curl);
//显示获得的数据
print_r($data);
(3)postman调用
参数
{"fileName":"E:/java/project/ppt/data/tmpTest.pptx","ops":[["E:/java/project/ppt/data/test1.pptx","1,2,9"],["E:/java/project/ppt/data/Threejs动画.pptx","1"]]}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~