java 为什么SpringDoc OpenAPI不理解MultipartFile负载?

zf9nrax1  于 2023-05-21  发布在  Java
关注(0)|答案(1)|浏览(182)

我正在开发一个Sping Boot 应用程序,它有一个控制器来帮助上传Multipart文件:

@PostMapping("/files")
public ResponseEntity<?> uploadFiles(
        @RequestParam("file") MultipartFile[] file, String comment) 
        throws IOException, ExecutionException, InterruptedException {
    log.debug("Total files to store: {}", file.length);
    log.debug("comment: {}", comment);
    fileService.storeFile(Arrays.asList(file), comment);
    return ResponseEntity.ok(environment.getProperty("file.upload.success"));
}

**问题:**OpenDocAPI(swagger)无法将此有效负载理解为文件。

如果我提到@RequestParam("file") MultipartFile fileString[],如果我使用MultipartFiles数组,它将显示此字段为String
我的Sping Boot 父项:

<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.1</version>

spring doc openapi依赖:

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-ui</artifactId>
    <version>1.6.9</version>
</dependency>

Swagger页面:

当我点击Try it out时,结果页面:

执行按钮不起作用
你知道我在控制器里漏掉了什么吗?

**PS -**我试着提一下:

@PostMapping(value = "/files", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})

但OpenAPI仍然只会将其视为String

e5nqia27

e5nqia271#

您可以使用@RequestPartMediaType.MULTIPART_FORM_DATA_VALUE
可以与OpenAPI中的@Parameterio.swagger.v3.oas.annotations)结合使用:

@PostMapping("/files", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
public ResponseEntity<?> uploadFiles(
    @Parameter(content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE))
    @RequestPart(value = "file") MultipartFile file,
    @RequestPart(value = "comment", required = false ) String comment
) throws IOException, ExecutionException, InterruptedException {
    log.debug("Total files to store: {}", file.length);
    log.debug("comment: {}", comment);
    fileService.storeFile(Arrays.asList(file), comment);
    return ResponseEntity.ok(environment.getProperty("file.upload.success"));
}

swagger-ui上,应在右侧显示选择的multipart/form-data

参考:

相关问题