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

2

8

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?

  • springdoc-openapi-ui 1.4.4
  • Spring Boot 2.3.2.RELEASE
  • spring-boot-starter-web
  • Maven
  • spring-boot-starter-data-jpa
Caprine answered 12/8, 2020 at 0:6 Comment(0)
C
3

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

Caprine answered 9/9, 2020 at 13:51 Comment(4)
How can this be an accepted answer, why would i send a json as a file !!Torn
@Torn No, JSON you send your data who is "personDTO"* like: name, age, email, etc, and the "file" you send your file, so this is mixed json with file sharing on an endpointCaprine
it's not an actual answer but maybe workaround, it's accepting json as well as a binary file: check this prnt.sc/X90pOyBMBaJ9Maypole
This is right solution actually, github.com/swagger-api/swagger-ui/issues/…Maypole
A
3

Following the link provided by @Vishal Zanzrukia

I added this configuration:

import java.util.ArrayList;

import org.springframework.boot.info.BuildProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;



@Configuration
public class OpenApiConfig {

    /**
     * 
     * This method is needed to allow sending multipart requests. For example, when an item is 
     * created together with an image. If this is not set the request will return an exception with:
     * 
     * Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content-Type 
     * 'application/octet-stream' is not supported]
     * 
     * @param converter
     */
    public OpenApiConfig(MappingJackson2HttpMessageConverter converter) {
        var supportedMediaTypes = new ArrayList<>(converter.getSupportedMediaTypes());
        supportedMediaTypes.add(new MediaType("application", "octet-stream"));
        converter.setSupportedMediaTypes(supportedMediaTypes);
    }
...

And now it seems to be working as expected with a code like this: Controller.java

    @PostMapping(path = "/", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Item> createItemWithoutImage(@Valid @RequestBody Item item) {
        Item createdItem = itemService.createItem(item);
        return ResponseEntity.status(HttpStatus.CREATED).body(createdItem);
    }

    @PostMapping(path = "/", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseEntity<Item> createItem(@Valid @RequestPart Item item,
            @RequestPart(name = "image", required = false) MultipartFile imageFile) {

        // DO Something with the image

        Item createdItem = itemService.createItem(item);
        return ResponseEntity.status(HttpStatus.CREATED).body(createdItem);
    }

This is how it looks in swagger: endpoint_in-swagger

Ashwell answered 20/9, 2023 at 6:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.