• SpringDoc上传附件或文件 - Swagger3


    摘要

    Swagger2 升级到 Swagger3 之后发现对于附件出现了问题。

    依赖

            <dependency>
                <groupId>org.springdocgroupId>
                <artifactId>springdoc-openapi-uiartifactId>
                <version>1.7.0version>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    问题描述

    在Swagger2时的样子:可以看到Request body是可以选择application/octet-stream
    在这里插入图片描述升级到Swagger3之后:可以看到Request body只有application/json能选择,也就是说请求体的类型被定死了。
    在这里插入图片描述## 解决方案
    问题解决之前的代码:

    @Operation(summary = "上传附件")
        @PostMapping("uploadAttachment")
        public H3yunObject uploadAttachment(@Parameter(description = "表单编码") @RequestParam("schemaCode") String schemaCode,
                                            @Parameter(description = "表单ObjectId值") @RequestParam("bizObjectId") String bizObjectId,
                                            @Parameter(description = "控件编码", example = "F0000001") @RequestParam("filePropertyName") String filePropertyName,
                                            @RequestBody MultipartFile file) throws IOException {
            log.info("文件上传参数:schemaCode = {}, bizObjectId = {}, filePropertyName = {}", schemaCode, bizObjectId, filePropertyName);
            if (file != null) {
                log.info("文件名:{}", file.getOriginalFilename());
            }
            return null;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    修改代码为:

    @Operation(summary = "上传附件")
        @PostMapping("uploadAttachment")
        public H3yunObject uploadAttachment(@Parameter(description = "表单编码") @RequestParam("schemaCode") String schemaCode,
                                            @Parameter(description = "表单ObjectId值") @RequestParam("bizObjectId") String bizObjectId,
                                            @Parameter(description = "控件编码", example = "F0000001") @RequestParam("filePropertyName") String filePropertyName,
                                            @io.swagger.v3.oas.annotations.parameters.RequestBody(
                                                    description = "附件",
                                                    content = @Content(
                                                            mediaType = "multipart/form-data",
                                                            schema = @Schema(type = "object"),
                                                            schemaProperties = {
                                                                    @SchemaProperty(
                                                                            name = "file",
                                                                            schema = @Schema(type = "string", format = "binary")
                                                                    )
                                                            }
                                                    )
                                            ) MultipartFile file) throws IOException {
            log.info("文件上传参数:schemaCode = {}, bizObjectId = {}, filePropertyName = {}", schemaCode, bizObjectId, filePropertyName);
            if (file != null) {
                log.info("文件名:{}", file.getOriginalFilename());
            }
            return null;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    测试修改过后的代码

    修改代码后重启应用,可以发现请求体成功变成multipart/form-data
    在这里插入图片描述选择一个文件点击执行
    在这里插入图片描述可以看到后端成功拿到文件了。
    在这里插入图片描述

    总结

    很简单的解决方案,把@RequestBody MultipartFile file这个参数的RequestBody注解换成swagger的就成了,目的是显示指定请求体具体字段的类型。

    换了之后变成

    
    @io.swagger.v3.oas.annotations.parameters.RequestBody(
                                                    description = "附件",
                                                    content = @Content(
                                                            mediaType = "multipart/form-data",
                                                            schema = @Schema(type = "object"),
                                                            schemaProperties = {
                                                                    @SchemaProperty(
                                                                            name = "file",
                                                                            schema = @Schema(type = "string", format = "binary")
                                                                    )
                                                            }
                                                    )
                                            ) MultipartFile file
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
  • 相关阅读:
    深入理解神经网络
    计算机系统概述之计算机的发展历程
    VB.NET媒体播放器PPT免费模板
    Spark 3.0 - 5.ML Pipeline 实战之电影影评情感分析
    基于ssm框架的(非maven)学生选课管理系统
    十二、Docker的简介
    SSH指定用户登录与限制
    Springboot——如何保证服务启动后不自动停止?
    Docker笔记-Debian容器内搭建ssh服务
    MySQL面试知识点总结(持续更新)
  • 原文地址:https://blog.csdn.net/qq_43657722/article/details/134057229