Springboot@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.JSONString());
System.out.("fileName"));
ArrayList ll= (("ops");
for(int i = 0;i < ll.size(); i ++){
ArrayList mm = ((i);
for(int j = 0;j < mm.size(); j ++){
System.out.(j));
}
}
SuccessResult();
}
(1)js调⽤
var dispatchesDTO = {"fileName":"E:/java/project/ppt/data/tmpTest.pptx","ops":[["E:/java/project/ppt/dat
a/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: "localhost:8080/api/ppt/combine",
data: JSON.stringify(dispatchesDTO),
success: function(data){
console.log(data);
}
});
(2)php的curl调⽤
<?php
//初始化
$curl = curl_init();
//设置抓取的url
curl_setopt($curl, CURLOPT_URL, '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);
springboot原理图解
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',
'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"]]}
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论