springmvc receives files and objects at the same time

  @RequestMapping("/importEmp")
    @ResponseBody
    public ResultJson importEmployeeFromExcel( String json, @RequestParam("file") MultipartFile file) throws Exception {
        User parse = JSON.parseObject(json, User.class);
        System.out.println(parse);
        employeeSystemService.importEmployeeFromExcel(file);
        ResultJson r = new ResultJson();
        r.setCode(EmployeeErrorCodeEnum.SUCCESS.getCode());
        r.setMsg(EmployeeErrorCodeEnum.SUCCESS.getDescription());
        return r;
    }

postman test

In form-data mode, carry the file json string and send it to the backend

 In this way, the backend can receive files and json

Note: If you use postman, you must add Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW in the Headers, if you write Content-Type: multipart/form-data; in the browser

Guess you like

Origin blog.csdn.net/qq_42058998/article/details/131799793