4

In Spring Boot project with Swagger 3 OpenAPI.

Have a POST method with Multipart file as RequestPart.

In swagger-ui ideally it should ask for file upload but only shows file as a String.

Kindly help me get file upload instead of String in swagger-ui.

My RestController:

@RestController
public class HelloController {
    @RequestMapping(method = RequestMethod.GET, value = "/api")
    public String sayHello() {
        return "Swagger Hello World";
    }
    @RequestMapping(method = RequestMethod.POST, value = "/fileUpload")
    public String fileUpload(
            @RequestPart(value = "file", required = false ) MultipartFile file,
            @RequestPart(value = "userName", required = false ) String userName
    ) {
        return "added successfully";
    }
}

File pom.xml:

<modelVersion>4.0.0</modelVersion>
<groupId>com.poc</groupId>
<artifactId>springboot-swagger-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.5.2</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-ui</artifactId>
        <version>1.6.12</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
    

On swagger-ui:

Swagger-ui screenshot

ℛɑƒæĿᴿᴹᴿ
  • 4,983
  • 4
  • 38
  • 58
TheCoder
  • 41
  • 5

2 Answers2

3

Add consumes= MediaType.MULTIPART_FORM_DATA_VALUE in the request mapping:

@RequestMapping(method = RequestMethod.POST, value = "/fileUpload", 
               consumes= MediaType.MULTIPART_FORM_DATA_VALUE)
public String fileUpload(
    @RequestPart(value = "file", required = false ) MultipartFile file,
    @RequestPart(value = "userName", required = false ) String userName
) {
    return "added successfully";
}

See below picture for output:

enter image description here

ℛɑƒæĿᴿᴹᴿ
  • 4,983
  • 4
  • 38
  • 58
shruti garg
  • 177
  • 1
  • 14
2

To enable file uploads in the Swagger UI, must add the @io.swagger.v3.oas.annotations.media.Schema annotation to your MultipartFile parameter.

Also you must set consume media type strictly your code must modify like this:

@RestController
public class HelloController {
    @RequestMapping(method = RequestMethod.GET, value = "/api")
    public String sayHello() {
        return "Swagger Hello World";
    }
    @PostMapping("/fileUpload",consumes= MediaType.MULTIPART_FORM_DATA_VALUE)
    public String fileUpload(
            @Parameter(description = "File to upload") @RequestPart(value = "file") 
            @Schema(type = "string", format = "binary") MultipartFile file,
            @Parameter(description = "User name") @RequestPart(value = "userName") String userName
    ) {
        return "added successfully";
    }
}
ℛɑƒæĿᴿᴹᴿ
  • 4,983
  • 4
  • 38
  • 58
Soheil Babadi
  • 562
  • 2
  • 4
  • 15