04 Swagger

Swagger 提供了一套通过代码和注解自动生成文档的方法。

添加依赖

<!-- 自动生成描述 API 的 json 文件 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>

<!-- 将 json 文件解析出来,用一种更友好的方式呈现 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

配置参数

swagger:
  title: API标题
  description: API描述
  version: 1.0.0
  terms-of-service-url: https://chanshiyu.com/
  base-package: com.chanshiyu
  contact:
    name: Chanshiyu
    url: https://chanshiyu.com/
    email: me@chanshiyu.com

配置类

使用

注解名称

使用说明

@Api

描述一个 API 类

@ApiImplicitParam

描述一个请求参数

@ApiImplicitParams

描述一组请求参数

@ApiModel

描述一个返回的对象

@ApiModelProperty

描述一个返回的对象参数

@ApiOperation

描述一个 API 方法

@ApiParam

描述一个方法的参数

@ApiResponse

描述一个请求响应

@ApiResponses

描述一组请求响应

@ApiIgnore

表示忽略

启动项目访问路径查看文档:http://192.168.51.242:8080/swagger-ui.html#/

参考文章: 在 Spring Boot 项目中使用 Swagger 文档

最后更新于

这有帮助吗?