当前位置: 首页 > news >正文

SpringBoot3整合OpenAPI3(Swagger3)

文章目录

  • 一、引入依赖
  • 二、使用
      • 1. @OpenAPIDefinition + @Info
      • 2. @Tag
      • 3. @Operation
      • 4. @Parameter
      • 5. @Schema
      • 6. @ApiResponse

swagger2更新到3后,再使用方法上发生了很大的变化,名称也变为OpenAPI3

官方文档

一、引入依赖

            <dependency><groupId>org.springdoc</groupId><artifactId>springdoc-openapi-starter-webmvc-ui</artifactId><version>${springdoc-openapi.version}</version></dependency>
server:servlet:context-path: /content
springdoc:api-docs:enabled: truepath: /v3/api-docsswagger-ui:enabled: truepath: /swagger-ui.html

openapi3使用十分方便,做到这里后,你可以直接通过以下网址访问swagger页面。

http://<ip>:<port>/content/swagger-ui/index.html

在这里插入图片描述

二、使用

1. @OpenAPIDefinition + @Info

用于定义整个 API 的信息,通常放在主应用类上。可以包括 API 的标题、描述、版本等信息。

@SpringBootApplication
@Slf4j
@OpenAPIDefinition(info = @Info(title = "内容管理系统", description = "对课程相关信息进行管理", version = "1.0.0"))
public class ContentApplication {public static void main(String[] args) {SpringApplication.run(ContentApplication.class, args);}
}

2. @Tag

用于对 API 进行分组。可以在控制器类或方法级别上使用。

@Tag(name = "课程信息编辑接口")
@RestController("content")
public class CourseBaseInfoController {
}

3. @Operation

描述单个 API 操作(即一个请求映射方法)。可以提供操作的摘要、描述、标签等。

    @Operation(summary = "课程查询接口")@PostMapping("/course/list")public PageResult<CourseBase> list(PageParams params,@RequestBody(required = false) QueryCourseParamsDto dto){CourseBase courseBase = new CourseBase();courseBase.setCreateDate(LocalDateTime.now());return new PageResult<CourseBase>(new ArrayList<CourseBase>(List.of(courseBase)),20, 2, 10);}

在这里插入图片描述

4. @Parameter

用于描述方法参数的额外信息,例如参数的描述、是否必需等。

    @Operation(summary = "课程查询接口")@PostMapping("/course/list")public PageResult<CourseBase> list(@Parameter(description = "分页参数") PageParams params,@Parameter(description = "请求具体内容") @RequestBody(required = false) QueryCourseParamsDto dto){CourseBase courseBase = new CourseBase();courseBase.setCreateDate(LocalDateTime.now());return new PageResult<CourseBase>(new ArrayList<CourseBase>(List.of(courseBase)),20, 2, 10);}

在这里插入图片描述

5. @Schema

描述模型的结构。可以用于类级别(标注在模型类上)或字段级别。

@Data
@AllArgsConstructor
@NoArgsConstructor
public class PageParams {//当前页码@Schema(description = "页码")private Long pageNo = 1L;//每页记录数默认值@Schema(description = "每页条目数量")private Long pageSize =10L;
}

在这里插入图片描述

6. @ApiResponse

描述 API 响应的预期结果。可以指定状态码、描述以及返回类型。

@ApiResponse(responseCode = "200", description = "Successfully retrieved user")
public User getUserById(@PathVariable Long id) {
}

在这里插入图片描述

http://www.lryc.cn/news/285472.html

相关文章:

  • 2023美赛各题分析,2024美赛数学建模思路解析2.2日第一时间更新
  • 分享一个学习git的网站
  • 用户拉新的4大关键策略,照着做就对了!
  • 如何用“VMware安装Ubuntu”win11系统?
  • ZJOI2009 对称的正方形
  • 大模型学习与实践笔记(十一)
  • Elasticsearch+Kibana 学习记录
  • Cesium叠加超图二维服务、三维场景模型
  • 【低危】OpenSSL 拒绝服务漏洞
  • TDL-Tiny Synopsis-TED-ED 网络理论 Network Theory
  • GIS项目实战08:JetBrains IntelliJ IDEA 2022 激活
  • Linux 命令大全 CentOS常用运维命令
  • 6.3.5编辑视频
  • 同星多通道CAN FD转USB/WIFI设备,解决近距离无线通讯问题
  • wamp环境的组成
  • Idea 开发环境不断切换git代码分支导致冲掉别人代码
  • GO 中如何防止 goroutine 泄露
  • Linux练习题
  • storm统计服务开启zookeeper、kafka 、Storm(sasl认证)
  • YOLOv8加入AIFI模块,附带项目源码链接
  • 【设计模式】代理模式的实现方式与使用场景
  • 医学图像的图像处理、分割、分类和定位-1
  • 【51单片机】外部中断
  • fastapi框架
  • 2023 年顶级前端工具
  • html 会跳舞的时间动画特效
  • 微信AR实现识别手部展示glb模型
  • MYSQL自连接、子查询
  • docker搭建hbase 全部流程(包含本地API访问)
  • Mybatis之关联