springboot post数组参数的方法

springboot post数组参数的方法

方式一

前端以字符串形式传递idList,采用逗号拼接,后端直接使用list接收 前端代码:

form: {
          
   
  otherParam: ,
  idList: [id1,id2].join(,)
}

后端代码:

// 在后端接收idList时,直接使用List<T> 就可以接收前端字符串(默认使用英文逗号,做自动切分)
@RequestMapping(value = "/updateXX", method = RequestMethod.POST)
public void updateXX(@RequestParam("otherParam") String otherParam, @RequestParam(value = "idList") List<String> idList) {
          
   
    
}

方式二

前端以数组形式传递,后端使用@RequestParam(value = "idList[]") List<String> idList方式接收 前端代码:

form: {
          
   
  otherParam: ,
  idList: [id1,id2]
}

后端代码:

@RequestMapping(value = "/updateXX", method = RequestMethod.POST)
public void updateXX(@RequestParam("otherParam") String otherParam, @RequestParam(value = "idList") List<String> idList) {
          
   
    
}

方式三

前端以json数组形式传递,后端使用@RequestBody List<String> idList方式接收

// 前端代码,采用post方式,以json数组传递数值
const idList= [13,22]
return axios({
          
   
  url: url,
  method: post,
  data: idList,
  headers: {
          
   
    Content-Type: application/json
  }
 })
// 使用@RequestBody方式接收
@PostMapping("/updateXX")
public void updateXX(@RequestBody List<String> idList) {
          
   
}
参考:https://blog..net/u012294724/article/details/117734941
经验分享 程序员 微信小程序 职场和发展