POST method with MultipartFile as RequestPart is considered as String in swagger3 open-api Springboot project
Asked Answered
S

2

6

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

Sendoff answered 21/10, 2022 at 5:53 Comment(0)
F
6

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

Flews answered 21/10, 2022 at 6:11 Comment(0)
F
3

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";
    }
}
Feed answered 18/5, 2023 at 16:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.