swagger技术
-
号称是世界上最流行的Api框架;
-
RestFul Api 文档在线自动生成工具==>Api文档自动更新;
-
直接运行,可以在线测试Api接口;
-
支持多种语言;(java,Php…);
官网:https://swagger.io/
在项目中使用Swagger需要架包springbox,其中需要两个jar包,
- swagger2
- swagger-ui
Springboot 集成swagger
-
新建立一个SpringBoot–web的项目
-
导入相关依赖
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency>
-
编写一个hellow请求。
-
配置swagger=====>书写config类
@Configuration @EnableSwagger2 // 开启swagger2 public class SwaggerConfig { }
-
启动项目访问:http://localhost:8080/swagger-ui.html#/hellow-control
-
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-aOalSaT7-1595126734546)(C:\Users\傻孩子\AppData\Roaming\Typora\typora-user-images\image-20200718153427032.png)]
配置Swagger
看源码配置,尝试改变源码来增加自己的理解。这就是渐渐的看源码的过程,是一个很好的学习的过程。
@Configuration
@EnableSwagger2 // 开启swagger2
public class SwaggerConfig {
// 配置swagger的Docker 的bean实例
@Bean
public Docket docket(){// 进行修改apiInfo来感受一下怎么去找源码,看源码。
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo());
}
// 根据源码来进行配置
public ApiInfo apiInfo(){
return
new ApiInfo(
"本人进行测试",
"未来的路很长,需要用心慢慢走,也需要快快的走",
"稳定版本1.0",
"urn:tos",
new Contact("张凯杰", "", ""),
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
}
}
Swagger配置接口扫描
Dcoket.select()接口
其中一个问题题,在开发环境中使用,在生产环境中不使用。
**开发环境:**开发环境是程序猿们专门用于开发的服务器,配置可以比较随意, 为了开发调试方便,一般打开全部错误报告。(也就是自己编码的时候的环境)
**测试环境:**一般是克隆一份生产环境的配置,一个程序在测试环境工作不正常,那么肯定不能把它发布到生产机上。(编码结束,进行测试bug时候的环境)
**生产环境:**是值正式提供对外服务的,一般会关掉错误报告,打开错误日志。(是测试结束后,上传到服务器上面之后供用户使用时的环境)
三个环境也可以说是系统开发的三个阶段:开发->测试->上线,其中生产环境也就是通常说的真实环境。
@Configuration
@EnableSwagger2 // 开启swagger2
public class SwaggerConfig {
// 配置swagger的Docker 的bean实例
@Bean
public Docket docket(Environment environment){
// Environment environment 用来获取当时的环境
// 设置要显示的 swagger 环境
Profiles profiles = Profiles.of("dev","text");
boolean flag = environment.acceptsProfiles(profiles);
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
// enable(false)是不是启动swagger。
// .enable(false)
.select()
// RequestHandlerSelectors,配置要扫描注解的接口方式
// basePackage:指定要扫描的包
// any():扫描全部
// none():不扫描
// withClassAnnotiontation:扫描类上的注解,参数是一个注解的反射对象
// withMethodAnnotation:扫描方法上的注解
.apis(RequestHandlerSelectors.basePackage("com.dream.swagger.control"))
// .path()过滤什么路径
.paths(PathSelectors.ant("/dream/**"))
.build();
}
public ApiInfo apiInfo(){
return
new ApiInfo(
"本人进行测试",
"未来的路很长,需要用心慢慢走,也需要快快的走",
"稳定版本1.0",
"urn:tos",
new Contact("张凯杰", "", ""),
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
}
}
配置API文档的分组
如何配置多个组;使用多个Docket实例对象,然后进行进行配置相关的Bean实例。
@Bean
public Docket docket1(){
return new Docket(DocumentationType.SWAGGER_2).groupName("A");
}
@Bean
public Docket docket2(){
return new Docket(DocumentationType.SWAGGER_2).groupName("B");
}
@Bean
public Docket docket3(){
return new Docket(DocumentationType.SWAGGER_2).groupName("C");
}
@Bean
public Docket docket4(){
return new Docket(DocumentationType.SWAGGER_2).groupName("D");
}
配置实体类
然后使用注解,注解使用推荐博客:https://www.cnblogs.com/JealousGirl/p/swagger2.html
总结:
- 我们可以通过swagger给一些比较难理解的属性或者接口,增加注释信息
- 接口文档实时更新
- 可以在线测试
本篇文档参考狂神视频:奉上大神视频链接
https://www.bilibili.com/video/BV1Y441197Lw?p=1