How to POST MultipartFile with additional information in one POST request in Spring Boot and Swagger?

Viewed 158

I have the following controller structure:

    @PostMapping(
            value = "/whatever",
            consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE
    )
    public ResponseEntity<ResponseDTO> method(@RequestBody RequestDTO dto, @RequestPart MultipartFile file) {
        ...
    }

Spring Boot version: 2.3.4.RELEASE

I am using swagger:

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>3.0.0</version>
</dependency>

Tried and failed:

  • With specifying the parameters as @RequestBody and @RequestPart, when using the two together I am getting error 415.
  • If I remove any of the inputs, the request works, so I wither upload file or DTO.
  • If I am using @RequestPart for both of them, Swagger does not display the DTO json input field.
  • If the MultipartFile is not annotated with @RequestPart, the upload button does not appear.
  • If both of the parameters are annotated with @RequestParam, the upload fails with error 415

Question: How can I upload a file with additional json information via Swagger?

0 Answers
Related