springboot 传List参数

最近项目有个需求,前台需要传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

Demo.java字段有:id,

@RequestMapping(value = "/demo/testListParam", method = RequestMethod.POST)
@ResponseBody
public ResponseVO> test(StAdjust stAdjust, @RequestParam("jsonStr")String listJSON) {
    
    List demoList = JSON.parseArray(listJSON,Demo.class);

 }

 

你可能感兴趣的:(springmvc,springboot)