OpenApi send MultipartFile request with JSON get 'application / octet-stream' error not supported

Viewed 5133

I'm using Spring Boot and I want send MultipartFile with json using Swagger UI but I receive the error 'application/octet-stream' error not supported,if I use Postman work very well.

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

How to resolve this?

  • springdoc-openapi-ui 1.4.4
  • Spring Boot 2.3.2.RELEASE
  • spring-boot-starter-web
  • Maven
  • spring-boot-starter-data-jpa
1 Answers

To send a json with multipartFile, use the annotation @Parameter with type "string" and format "binary", so that you can send a file with format json.

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

And then it will be like this.

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

Reference - Multipart Request with JSON - GitHub Springdoc openApi

Related