[SWAGGER 2 UI]: Enabling multipart / form-data requests

Viewed 1735

I have a simple controller method that captures some JSON information as well as an arbitrary number of uploaded image files:

@PostMapping(value = "/products", consumes = MediaType.MULTIPART_FORM_DATA_VALUE )
public ResponseEntity<ResponseMessage> postProduct(@RequestPart(name = "request") final MyJsonBody postRequest,
                                                   @RequestPart(name = "images") final ist<MultipartFile> images)
{
    log.info("Request id and name fields: " + postRequest.getProductId() + ", " + postRequest.getProductName() + ".");
    log.info("Received a total of: " + images.size()  + " files.");
    return success("Just wanted to test the upload functionality!", null, HttpStatus.OK);   // A private method I have to return an appropriate ResponseEntity<> to the user.
}

The MyJsonBody class is a simple POJO for testing purposes:

@Data
@Builder(access = PUBLIC)
@AllArgsConstructor
@NoArgsConstructor
public class MyJsonBody
{
    @JsonProperty("id") private String productId;
    @JsonProperty("name") private String productName;
}

When using Postman, my example multipart/form-data POST request works just fine:

My Postman request structure

As you can see here, Springboot is completely fine with the request and prints the data as expected:

2020-12-21 14:28:20.321  INFO 11176 --- [nio-8080-exec-3] c.j.r.fileuploads.FileUploadController   : Request id and name fields: RANDOM_ID, null.
2020-12-21 14:28:20.321  INFO 11176 --- [nio-8080-exec-3] c.j.r.fileuploads.FileUploadController   : Received a total of: 2 files.

My team uses Swagger 2, and it's important to me to be able to debug the API using the Swagger UI. Unfortunately, when sending an identical request from the UI:

Swagger request structure

The UI appears to not be sending the request as a multipart/form-data request, but as an application/octet-stream request:

2020-12-21 14:27:40.095  WARN 11176 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/octet-stream' not supported]

Most information on the Web concerns the OpenAPI spec version 3, yet we are using version 2.8 for now. Any ideas about how I might be malforming the request?

Edit: The following is my configuration class for Swagger. All of my code in this toy projects is under package com.jason.rest.fileuploads.

@Configuration
@EnableSwagger2
public class SwaggerConfig extends WebMvcConfigurationSupport {
    @Bean
    public Docket productApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select().apis(RequestHandlerSelectors.basePackage("com.jason.rest.fileuploads"))
                .paths(PathSelectors.any())
                .build();
    }

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    @Bean
    public LinkDiscoverers discoverers() {
        List<LinkDiscoverer> plugins = new ArrayList<>();
        plugins.add(new CollectionJsonLinkDiscoverer());
        return new LinkDiscoverers(SimplePluginRegistry.create(plugins));

    }

}

0 Answers
Related