最近项目有个需求,前台需要传list参数请求controller接口,一开始直接使用ResponseBody注解,但实践下来发现参数没有传到controller。
现将处理方式记录如下:
1、前台
将list参数转换为json字符串: JSON.stringify(list)
var list=[]; list.push({ "id":1, "type":2, }); list.push({ "id":2, "type":3, });
this.$http.post("/demo/testListParam",{"jsonStr":JSON.stringify(list),"id":parseInt(this.id),"reason":this.reason} , { }).then(res => { if (res.resultCode == 200 ) { console.log("data:"+res.data); } else { setTimeout(() => { this.instance("error", "提示", res.message); }, 500); } });
2、controller
通过String拿到json字符串,再将json字符串转为List.
StAdjust类有字段id、reason
@RequestMapping(value = "/demo/testListParam", method = RequestMethod.POST)
@ResponseBody
public ResponseVO<Map<String,Object>> test(StAdjust stAdjust, @RequestParam("jsonStr")String listJSON) {
List<Demo> demoList = JSON.parseArray(listJSON,Demo.class);
}
var taskList = ["123","456"]; var params = { taskList: taskList }; $.ajax({ type: "PUT", dataType: "json", url: "/client/update", data: params, success: function (msg) { } });
1 @RequestMapping(value = "/update", method = RequestMethod.PUT) 2 @ResponseBody 3 public JSONResult updateClient(Client client, @RequestParam(value = "taskList[]") List<String> taskList) { 4 logger.debug("Yufan taskList={}", taskList); 5 return JSONResult.ok(); 6 }
发送的参数Json数据:
{"id":1, "shiftDetails":[{"id":1,"lineId":2}], "broadcasts":[{"id":1,"lineId":1,"imgPath":"TEST1"}, { "id":2,"lineId":2,"imgPath":"TEST2"}] }
Controller类:
@Controller @RequestMapping("/travelLine") public class TravelLineController { @RequestMapping("/insertTest") @ResponseBody public Json insertTest(@RequestBody TravelLineDomain tl) { Json j = new Json();