spring-bootmavenswaggerswagger-uiopenapi

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


I'm using Spring Boot and I want to 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.

@ResponseBody
@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 do I solve this?


Solution

  • 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