Swagger
优点:
- 号称世界上最流行的Api框架
- RestFul Api文档在线自动生成工具=>Api文档与Api定义同步更新
- 直接运行,在线测试Api接口
- 支持多种语言
官网:https://swagger.io/
在项目中使用Swagger需要springfox
- Swagger2
- ui
SpringBoot集成Swagger
- 新建springboot-web项目
- 添加依赖
<!--导入swagger依赖-->
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- 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>
- 配置Swagger
//加了注解就会配置到配置里
@Configuration
@EnableSwagger2 //开启Swagger2
public class SwaggerConfig {
}
- 配置Swagger信息
//配置 Swagger 信息 = apiInfo
private ApiInfo apiInfo() {
//作者信息
Contact contact = new Contact("kkk", "https://www.baidu.com", "dawd@qq.com");
return new ApiInfo("学习笔记",
"Api Documentation", "1.0", "https://blog.csdn.net/qq_42042158/article/details/121686751",
contact, "Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList<VendorExtension>());
}
- 配置是否启动swagger以及扫描指定包
//配置了 Swagger 的 Docket 的 Bean 实例
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
//是否启用swagger
.enable(true)
.select()
//配置要扫描接口的方式
//basePackage: 指定要扫描的包
//any():扫描全部
//none():不扫描
//withClassAnnotation(): 扫描类上的注解
.apis(RequestHandlerSelectors.basePackage("com.kang.swagger.controller"))
//过滤 具体方式再指定
//.paths(PathSelectors.ant("/kang/**"))
.build(); //创造什么东西
}
-
配置API文档的分组
.groupName("kkkk")
参考视频:https://www.bilibili.com/video/BV1PE411i7CV