以MultipartFile为RequestPart的POST方法在swagger3 open-api Springboot项目中被视为String

2024-04-14

在使用 Swagger 3 OpenAPI 的 Spring Boot 项目中。

Have a POST方法与Multipart文件为RequestPart.

In swagger-ui理想情况下,它应该要求文件上传,但仅将文件显示为String.

请帮助我上传文件而不是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:


Add consumes= MediaType.MULTIPART_FORM_DATA_VALUE在请求映射中:

@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";
}

输出见下图:

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

以MultipartFile为RequestPart的POST方法在swagger3 open-api Springboot项目中被视为String 的相关文章

随机推荐