在最近做的一個項目里面,涉及到一個批量導出學生信息到excel文件的需求,發現在controller的參數封裝不了對象數組,一直是null
解決方案:
我們后端接收采用 String 類型 接收一個json的字符串
前端也就傳遞給我們一個json數據
在通過處理json數據獲取我們想要得到的對象數組
廢話不多說上代碼
Controller
public Result exportStudents(@RequestBody String students, HttpServletResponse response){ JSONObject jsonObject = JSONObject.parseObject(students); List<StudentDto> studentDtos = JSONObject.parseArray(jsonObject.getJSONArray("students").toJSONString(), StudentDto.class); return null; }
Vue + axios 傳參
const{data:res}=await this.$axios.post("/student/exportStudents",{students:對象數組})