Spring Boot OpenApi使用JSON发送MultipartFile请求

93ze6v8z  于 2023-04-11  发布在  Spring
关注(0)|答案(1)|浏览(190)

还有比这更好的解决方案吗(OpenApi send MultipartFile request with JSON get 'application / octet-stream' error not supported)?

@PostMapping(path = "", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@Operation(summary = "XX", requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(description = "Das zu erstellende Ticket", content = @Content(schema = @Schema(implementation = TicketDTO.class))))
TicketDTO createTicket(@RequestPart() @Parameter(required = true) TicketDTO ticket,
        @Parameter(description = "Files to be uploaded", content = @Content(mediaType = MediaType.MULTIPART_FORM_DATA_VALUE)) @RequestPart(value = "files", required = false) MultipartFile[] files)
        throws MessagingException;

这对我不起作用。我在Swagger UI中看不到上传文件的选项。

jgwigjjp

jgwigjjp1#

您不需要在@Operation中设置requestBody。
范例

@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  @Operation(summary = "XX")
  public TicketDTO createTicket(@RequestPart() @Parameter(required = true) TicketDTO ticket,
                         @Parameter(description = "Files to be uploaded", content = @Content(mediaType = MediaType.MULTIPART_FORM_DATA_VALUE)) @RequestPart(value = "files", required = false) MultipartFile[] files) {
    return ticket;
  }

相关问题