这里写自定义目录标题
一、Swagger简介
前后端分离
- 前后端通过API进行交互
- 前后端相对独立且松耦合
产生的问题
- 前后端集成,前端或者后端无法做到“及时协商,今早解决”,最终导致问题集中爆发
解决的方案
- 实时跟踪API,降低集成风险
Swagger
- 最流行的API框架
- Restful API 文档在线自动生成器:API文档与API定义同步更新
- 直接运行,在线测试API
- 支持多种语言
- 官网:https://swagger.io/
二、依赖导入
<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>
三、编写Swagger配置类SwaggerConfig
要使用swagger,我们需要编写一个配置类SwaggerConfig来配置Swagger
@Configuration
@EnableSwagger2
public class SwaggerConfig {
}
3.1启动项目访问localhost:8080/swagger-ui.html
四、配置Swagger
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket docket1(){
return new Docket(DocumentationType.SWAGGER_2).groupName("秦羽");
}
@Bean
public Docket docket2(){
return new Docket(DocumentationType.SWAGGER_2).groupName("萧炎");
}
@Bean
public Docket docket3(){
return new Docket(DocumentationType.SWAGGER_2).groupName("李星云");
}
@Bean
public Docket docket(Environment environment){
Profiles profiles = Profiles.of("dev","test","pro");
boolean flag = environment.acceptsProfiles(profiles);
System.out.println("flag="+flag);
return new Docket(DocumentationType.SWAGGER_2)
//Docket实例关联上APIInfo
.apiInfo(apiInfo())
//组名
.groupName("不良帅")
//配置是否启用swagger,如果是false,在浏览器无法访问swagger
.enable(flag)
.select()//通过.select()方法,去配置扫描接口,RequestHandlerSelectors配置如何扫描接口
.apis(RequestHandlerSelectors.basePackage("com.liao.controller"))
.build();//包名com.liao.controller扫描
}
//通过apiInfo()属性配置文档信息
public ApiInfo apiInfo(){
Contact contact = new Contact("荒天帝石昊","https://www.bilibili.com/video/BV1PE411i7CV?p=48&spm_id_from=pageDriver&vd_source=9985799e05ec49cb830fd18fcc5cbf9c","1356805992@qq.com");
return new ApiInfo(
"完美世界",//标题
"lehu",//描述
"2.0",//半本
"urn:tos",//组织链接
contact,//联系人信息
"Apache 2.0",//许可
"许可链接",//许可链接
new ArrayList<VendorExtension>());//拓展
}
}