前言
前后端分离时代前端后端的问题: ·前后端集成联调,前端和后端人员无法做到“即时协商,尽早解决”,最终导致问题集中爆发 解决方案: ·首先指定schema【计划的提纲】,实时更新API,降低集成风险 ·前后端分离: ·前端测试后端接口 ·后端提供接口,需要实时更新最新的消息及改动 Swagger ·号称世界上最流行的API框架 ·RestFul Api文档在线自动生成工具=>Api文档与Api定义同步更新 ·直接运行,可以在线测试API接口 ·支持多语言 官网:https://swagger.io/tools/swagger-ui/一、SpringBoot集成Swagger
·新建一个SpringBoot项目,导入依赖<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>
·写一个hello工程
·配置swagger
@Configuration
@EnableSwagger2
public class SwaggerConfig{
}
·测试运行:http://localhost:8080/swagger-ui.html
二、配置swagger
###swagger的bean实例Docket
@Configuration
@EnableSwagger2
public class SwaggerConfig{
@Bean
public Docket docket(){
return new Docket(Documentation.SWAGGER_2).apiInfo();
}
//配置swagger信息=apiInfo
private ApiInfo apiInfo(){
//作者信息
Contact contact = new Contact("秦疆","url","email")
return new ApiInfo(
"title",
"description",
"version",
"termsOfServiceUrl",
contact,
"license",
"licenseUrl"
new ArrayList()
);
}
}
Swagger配置扫描接口
Docket.select()
@Configuration
@EnableSwagger2
public class SwaggerConfig{
@Bean
public Docket docket(){
return new Docket(Documentation.SWAGGER_2)
.apiInfo()
.enable(true)//是否启动swagger
.select()
//RequestHandlerSelectors,配置要扫描接口的方式
//basePackage,指定要扫描的包,any()扫描全部,none不扫描,withClassAnnotion:扫描类上的注解,withMethodAnnotion:扫描方法上的注解
.apis(RequestHandlerSelectors.basePackage("包名"))
//path过滤
.path(PathSelectors.ant("路径"))
.build();
}
//配置swagger信息=apiInfo
private ApiInfo apiInfo(){
//作者信息
Contact contact = new Contact("秦疆","url","email")
return new ApiInfo(
"title",
"description",
"version",
"termsOfServiceUrl",
contact,
"license",
"licenseUrl"
new ArrayList()
);
}
}
提问:我只希望我的swagger在生产环境使用,在发布的时候不使用?
·判断是不是生产环境
·注入enable(flag)