OpenApi使用JSON发送MultipartFile请求不支持'application / octet-stream'错误

问题描述

我正在使用Spring Boot,我想使用Swagger UI将带有json的multipartfile发送给我,但是如果我使用Postman的情况很好的话,我会收到错误'application/octet-stream' error not supported

@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();
 }

该如何解决

解决方法

要发送带有multipartFile的json,请使用类型为@Parameter且格式为"string"的注释"binary",以便您可以发送格式为json的文件。

@Parameter(schema =@Schema(type = "string",format = "binary"))

然后就是这样。

@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;
}

参考-Multipart Request with JSON - GitHub Springdoc openApi