这篇文章主要介绍了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调用
<?php //初始化 $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', '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"]]}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。