快速理解
Swagger 可以定义标准的RESTful风格的API,API规范。
Swagger官网
Swagger GitHub地址
菜鸟教程
Swagger 的核心组件包括:
- Swagger UI:一个可视化界面,用于交互式地查看和测试 API。
- Swagger Editor:一个在线编辑器,支持实时预览 API 文档。
- Swagger Codegen:一个代码生成工具,可以根据 API 定义自动生成客户端或服务端代码。
- Swagger Hub:一个集成的 API 设计和文档平台,提供协作功能和云存储。
Swagger 的核心功能:
- 开发 API 支持两种开发模式:
- 代码优先:从已有代码生成 OpenAPI 文档。
- 设计优先:通过 Swagger Editor 先设计 API,再通过 Swagger Codegen 生成代码。
- 交互 API 文档
Swagger UI,支持通过浏览器直接与 API 交互、发送请求、查看响应,极大地方便了开发者和测试人员。
示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| openapi: 3.0.0 info: title: Sample API description: API description version: 1.0.0 servers: - url: https://api.example.com/v1 paths: /users: get: summary: Returns a list of users responses: '200': description: A JSON array of user names content: application/json: schema: type: array items: type: string
|
OpenAPI 规范的结构
OpenAPI 文档通常以 YAML 或 JSON 格式编写。
下面是一个基本的 OpenAPI 文档结构:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| openapi: 3.1.0 info: title: 示例 API description: 这是一个示例 API 文档 version: 1.0.0
servers: - url: https://api.example.com/v1 description: 生产环境 - url: https://dev-api.example.com/v1 description: 开发环境
paths: /users: get: summary: 获取所有用户 description: 返回系统中的所有用户列表 operationId: getUsers tags: - users parameters: - name: limit in: query description: 返回结果数量限制 schema: type: integer default: 20 responses: '200': description: 成功返回用户列表 content: application/json: schema: type: array items: $ref: '#/components/schemas/User' '400': description: 错误的请求参数
components:
schemas: User: type: object properties: id: type: integer format: int64 name: type: string email: type: string format: email required: - id - name
|
Springfox (2020年3.0.0 版本之后再无更新被SpringDoc代替)
Springfox 开源API Doc的框架,前身是swagger-springmvc
可以将 Controller 中的方法以文档的形式展现。
官方定义为: Automated JSON API documentation for API’s built with Spring。
Swagger 的使用
引入依赖
1 2 3 4 5 6 7 8 9 10 11
| <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency>
|
新建config/Swagger配置类
警告
线上环境不要把接口暴露出去!!!
可以通过在 SwaggerConfig 配置文件开头加上 @Profile({"dev", "test"}) 限定配置仅在部分环境开启
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
@Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket docket() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors .basePackage("com.yupi.usercenter.controller")
).paths(PathSelectors.any() ).build(); }
private ApiInfo apiInfo() { Contact contact = new Contact("wgzc", "https://wzcwzc10.github.io/", "3412399241@qq.com"); return new ApiInfoBuilder().title("泡泡伙伴匹配系统-接口文档") .description("你若盛开,清风徐来") .termsOfServiceUrl("https://github.com/wzcwzc10") .version("1.0") .license("Swagger-的使用(详细教程)").licenseUrl("https://github.com/wzcwzc10").contact(contact).build(); } }
|
SpringBoot 版本 >= 2.6 大概率会报错:
Failed to start bean 'documentationPluginsBootstrapper';
在 application.yaml 中添加配置:
1 2 3 4 5
| spring: mvc: pathmatch: matching-strategy: ANT_PATH_MATCHER
|
运行此地址URLhttp://localhost:8080/api/swagger-ui.html