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"]]}

 

posted @ 2019-05-08 13:45  慕尘  阅读(8642)  评论(0编辑  收藏  举报