如何在 Swagger 中解决这个问题 https://stackoverflow.com/questions/16015548/how-to-send-multipart-form-data-request-using-postman

How to Solve in Swagger this problem https://stackoverflow.com/questions/16015548/how-to-send-multipart-form-data-request-using-postman

我在静态添加文件时遇到此错误 api。

2021-10-18 10:21:36.556  WARN 15128 --- [nio-8080-exec-9] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'file' is not present]

这是我休息的地方api。

@ApiOperation(value = "Send file",
            notes = "This method accept a file")
    @PostMapping("/upload-file")
    public ResponseEntity<? extends Response> writeExcelledRowColumnIntoDatabase(@ApiParam(
            name = "file-name",
            type = ".xlsx",
            value = "Pass File Name",
            example = "file.xlsx",
            required = true) @RequestParam("file") MultipartFile pathFile) throws IOException {

        return this.userDetailService.acceptExcellFileAndInsertToDatabase(pathFile);

    }

我在网上找到了一些方法可以做到这一点,但我仍然遇到同样的错误。有什么办法可以解决这个问题吗?

您正在代码中使用参数“文件”@RequestParam("file")

并且 swagger 文档有 file-name

将函数参数更新为:

 @RequestParam("file-name") MultipartFile pathFile