Swagger快速入门教程笔记

现在市面上大多数公司都摒弃了传统 jsp 开发,采用前后端分离式的开发规则,前端使用 Vue,Angular,React 等等完成页面,后端省掉了视图跳转的过程,直接书写接口返回 json 数据供前端调用即可

这样一来就诞生了一个新的问题,后端程序员需要写一个接口文档来告诉前端开发人员都有那些接口,每个接口都是干什么的,需要那些参数等等。

书写接口文档是一件费时费力的活,而 Swagger 可以根据程序代码自动生成在线接口文档,Swagger 是接口文档生成工具

整合Swagger

教程笔记基于Springboot2.x工程进行测试

导入依赖

想要整合使用 Swagger 生成接口文档,首先我们需要引用 Swagger 的 maven 依赖:

<!-- 位于io.springfox下的依赖 --> <dependency> <!-- Swagger的注解依赖包 --> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>3.0.0</version> </dependency> <dependency> <!-- Swagger接口文档页面包 --> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>3.0.0</version> </dependency>

实际测试运行时程序会抛出For input string: ""异常,但是并不会影响运行,这里针对异常可以选择这样导包:

<!-- Swagger依赖 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> <!-- 排除下面两个依赖,解决 For input string: "" 异常 --> <exclusions> <exclusion> <groupId>io.springfox</groupId> <artifactId>swagger-annotations</artifactId> </exclusion> <exclusion> <groupId>io.swagger</groupId> <artifactId>swagger-models</artifactId> </exclusion> </exclusions> </dependency> <!-- 单独引用版本偏低的两个依赖 --> <dependency> <groupId>io.swagger</groupId> <artifactId>swagger-annotations</artifactId> <version>1.5.21</version> </dependency> <dependency> <groupId>io.swagger</groupId> <artifactId>swagger-models</artifactId> <version>1.5.21</version> </dependency> <!-- UI页面展示 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> 启用Swagger

在引用 Swagger 的依赖后,我们还需要通过注解开启 Swagge 才可以实现接口文档

新建一个配置类,通过 @EnableSwagger2 注解启用 Swagger:

@Configuration @EnableSwagger2 // 开启Swagger public class SwaggerConfig { }

开启Swagger后启动程序,访问:8080/swagger-ui.html即可看到接口文档页面

了解接口文档

Swagger 接口文档主要有四部分组成:分组信息、分组描述信息、接口描述信息、实体类信息

在这里插入图片描述

我们目前仅仅是引入了 Swagger 的依赖,开启 Swagger 功能之后如果没有配置的话,默认会使用 swagger 初始化的配置

初始化分组

我们想要使用自定义的分组信息,要在配置类提供一个 Docket 实例到 IOC 容器中,通过 Docket 实例设置分组名称,Swagger 会根据实例进行自定义设置。

创建一个分组

// 还是之前的配置类 @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket docket() { return new Docket(DocumentationType.SWAGGER_2) .groupName("张涵哲的分组"); } }

上面使用 Swagger2 默认规则创建了一个 Docket 对象,定义分组名称为 张涵哲的分组,效果如图所示:

在这里插入图片描述

多分组配置

如果想要创建多个分组,那么就在 IOC 容器中多提供几个 docket 实例:

@Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket docket() { return new Docket(DocumentationType.SWAGGER_2) .groupName("张涵哲的分组"); } @Bean public Docket docket1() { return new Docket(DocumentationType.SWAGGER_2) .groupName("***的分组"); } }

在这里插入图片描述

配置分组详情

配置分组描述

我们已经可以创建多个分组了,但是我们可以发现,每个分组中都有一段描述信息,我们可以在每个分组下显示不同的描述信息,需要调用 Docket 的 apiInfo() 函数传入自定义的配置。

@Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket docket() { return new Docket(DocumentationType.SWAGGER_2) .groupName("张涵哲的分组") .apiInfo(apiInfo()); } // 创建一个函数用来返回 ApiInfo 实例 // 这里我只显示了部分信息,填写null的都是不显示的,如果想要全部显示可以填写所有的信息 public ApiInfo apiInfo() { Contact contact = new Contact("张涵哲", "http://blog.hanzhe.club", null); return new ApiInfo( "基于Swagger2.0练习", "基于程序中所有的接口提供帮助文档", "1.0.0", null, contact, null, null, new ArrayList()); } }

我们为 张涵哲的分组 配置了一段描述信息,接下来看看效果:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zwjzgs.html