Swagger3中 form-data案例中exmple示例值失效的一次记录

Swagger3中 exmple示例值失效的一次记录

pom

<dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId>
            <version>4.4.0</version>
</dependency>

controller如下

import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;


@PostMapping(value = "/add", consumes = {"multipart/form-data"})
    @Operation(summary = "Add a new show")
    public WebResult<WebResponseBody<Long>> addShow(
            @Parameter(description = "showName", example = "The Great Show", required = true)   String showName,
            @Parameter(description = "Type of the show", example = "100", required = true) Integer showType,
            @Parameter(description = "Number of sessions", example = "5", required = true) Integer sessionNum,
            @Parameter(description = "Time of the first session", example = "2022-11-01 23:24:12", required = true)
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime firstSessionTime,
            @Parameter(description = "Time of the last session", example = "2022-11-01 23:24:12", required = true)
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime lastSessionTime,
            @Parameter(description = "Duration of the show in minutes", example = "ssad", required = true) Integer showDuration,
            @Parameter(description = "Description of the show", example = "This is an epic show of drama and performance.", required = true) String showDescription,
            @Parameter(description = "Venue ID", example = "adsad", required = true) Long venueId,
            @Parameter(description = "Show poster file name", example = "example.jpg", required = true) MultipartFile poster

    ) {
        AddShowDto addShowDto = new AddShowDto();
        addShowDto.setShowName(showName);
        addShowDto.setShowType(showType);
        addShowDto.setSessionNum(sessionNum);
        addShowDto.setFirstSessionTime(firstSessionTime);
        addShowDto.setLastSessionTime(lastSessionTime);
        addShowDto.setShowDuration(showDuration); 
        addShowDto.setShowDescription(showDescription);
        addShowDto.setVenueId(venueId);
        addShowDto.setPoster(poster);

        // Assume service call to process addShowDto and return result
        Long result = showService.addShow(addShowDto);
        return WebResult.success(result);
    }

问题:在swagger-ui中,只有Integer类型的example(示例值生效,且为必填项)

在这里插入图片描述
在这里插入图片描述

解决: 加多一个@RequestParam注解,为了统一,正常显示Integer类型也加了该注解。

在这里插入图片描述
在这里插入图片描述

测试的时候就不用一个个输入数据啦

在这里插入图片描述

最后

参考文章

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Swagger支持使用multipart/form-data作为Content-Type来传输请求体。在2.0版本,使用多部分内容时,必须使用schema来定义操作的输入参数。这种方式支持复杂的结构以及多文件上传的机制。在Spring Boot,可以使用springdoc相关接口代码来定义使用multipart/form-data的接口。例如,可以使用@PostMapping注解定义一个上传文件的接口,使用@RequestBody注解以及相应的content属性来指定参数的类型和格式。下面是一个示例代码: ``` @Operation(summary = "上传文件") @PostMapping("/files/upload") public CommonResult<String> upload( @RequestBody(content = { @Content( mediaType = "multipart/form-data", schema = @Schema(type = "object"), schemaProperties = { @SchemaProperty( name = "multipartFile", schema = @Schema(type = "string", format = "binary") ), @SchemaProperty( name = "info", schema = @Schema(type = "object") ) } ) }) MultipartFile multipartFile, String info ) { return CommonResult.success(multipartFile.toString() + info); } ``` 这个接口使用了multipart/form-data作为Content-Type,接受一个文件参数multipartFile和一个字符串参数info。在返回结果,可以根据需求修改CommonResult的泛型类型。123 #### 引用[.reference_title] - *1* *2* *3* [Springdoc、OpenApi如何编写multipart/form-data、文件上传接口](https://blog.csdn.net/HHoao/article/details/125767378)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值