一、Swagger配置扫描接口
沿用上一篇博客项目
Docket.select();
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//RequestHandlerSelectors:配置要扫描接口的方式
//basePackage("包名"):指定要扫描的包
//any():扫描全部
//none():不扫描
//withClassAnnotation():扫描类上的注解,参数是一个注解的反射对象
//withMethodAnnotation():扫描方法上的注解
.apis(RequestHandlerSelectors.basePackage("com.massimo.swagger.controller"))
.paths(PathSelectors.ant("/massimo/**"))
.build();
}
二、配置是否启动Swagger
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
//enable(),是否启动Swagger,默认为true,如果为false,则Swagger不能在浏览器中访问
.enable(false)
.select()
.apis(RequestHandlerSelectors.basePackage("com.massimo.swagger.controller"))
.build();
}
效果
三、根据环境使用Swagger
要求Swagger在生产环境中使用,在发布环境中不使用
思路:
1、判断是不是生产环境 flag = false
注入enable(flag)
3.1、添加环境
3.2、SwaggerConfig类
@Bean
public Docket docket(Environment environment){
//设置要显示的Swagger环境
Profiles profiles = Profiles.of("dev", "test");
//通过environment.acceptsProfiles()判读是否处在自己设定的环境中
boolean flag = environment.acceptsProfiles(profiles);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
//enable(),是否启动Swagger,默认为true,如果为false,则Swagger不能在浏览器中访问
.enable(flag)
.select()
.apis(RequestHandlerSelectors.basePackage("com.massimo.swagger.controller"))
.build();
}
效果: