springboot 传List参数

本文介绍了SpringBoot如何处理前端通过JSON字符串或表单方式传递的List参数。包括Controller中如何接收转化为List,以及前端两种JS AJAX提交List的方法。详细讲解了两种不同的接收方式。
摘要由CSDN通过智能技术生成
最近项目有个需求,前台需要传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);
 
 }

 






SpringBoot前端给后端传list

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();

  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值