@RequestParam接收前台传递过来的数组

 

有时候我们会遇到前端传递过来的数据是["123","fawef","faweion"]这种格式的数据如果参数名称是userNames

 

我们可以在后台用@RequestParam("userNames[]") List> userName来接收参数

 

@RequestMapping("/lowerUsers")
public R lowerUsers(@RequestParam("roleCodes[]") List roleCodes){
   logger.info("查询当前登录用户的下级用户========");
   if (getUserId() == null){
      return R.error("没有用户权限!");
   }
   Map param = new HashMap<>();
   param.put("createId", getUserId());
   param.put("roleCodes", roleCodes);
   List entity = loginUserService.queryLowerUserRole(param);
   List> mapList = new ArrayList<>();
   for (LoginUserTbEntity user:entity){
      Map map = new HashMap<>();
      map.put("userId", user.getId());
      map.put("username", user.getUsername());
      mapList.add(map);
   }
   return R.ok().put("list", mapList);
}

 

 

 

 

@RequestBody String[] userNames

 

@RequestMapping("/delete")
public R delete(@RequestBody String[] userNames){
   originalFilterTaskTbService.deleteBatch(ids);
   
   return R.ok();
}

 

 

 

如果有userid,userNames(这是数组),age三个参数 当然如果参数比较多我们可以用

@RequestParam MultiValueMap params来接收
取值的方式是
params.getFirst("userid")
params.getFirst("age")
List list = ((LinkedList) params.get("userNames[]"));
就是这样,以上我只是阐述方法并不建议这样接收参数,在这里记录一下被坑过的地方

 

你可能感兴趣的:(数组,@RequestParam)