OpenApi 使用 JSON 发送 MultipartFile 请求获取“应用程序/八位字节流”错误不受支持

Bra*_*lva 7 maven swagger swagger-ui spring-boot openapi

'application/octet-stream' error not supported我正在使用 Spring Boot,我想使用 Swagger UI 发送带有 json 的 MultipartFile,但如果我使用 Postman 工作得很好,我会收到错误。

@RequestMapping(value = "/upload", method = RequestMethod.POST,
produces = { "application/json" },
consumes = { "multipart/form-data" })
public String hello(
   @RequestPart(value = "file") MultipartFile file,
   @RequestPart("grupo") Grupo grupo) {
      if (file != null) {
        logger.info("File name:  " + file.getOriginalFilename());
      }
      logger.info(grupo.toString());
   return grupo.toString();
 }
Run Code Online (Sandbox Code Playgroud)

如何解决这个问题?

  • springdoc-openapi-ui 1.4.4
  • Spring Boot 2.3.2.RELEASE
  • spring-boot-启动器-web
  • 梅文
  • spring-boot-starter-数据-jpa

Bra*_*lva 3

要使用 multipartFile 发送 json,请使用@Parametertype"string"和 format注释"binary",以便您可以发送格式为 json 的文件。

@Parameter(schema =@Schema(type = "string", format = "binary"))
Run Code Online (Sandbox Code Playgroud)

然后就变成这样了。

@PostMapping(value = "/test", consumes = MediaType.MULTIPART_FORM_DATA_VALUE )
public ResponseEntity<Void> saveDocu2ment(
        @RequestPart(value = "personDTO") @Parameter(schema =@Schema(type = "string", format = "binary")) final PersonDTO personDTO,
        @RequestPart(value = "file")  final MultipartFile file) {
    return null;
}
Run Code Online (Sandbox Code Playgroud)

参考 -使用 JSON 的多部分请求 - GitHub Springdoc openApi