通过配置文件(推荐)
在相应的springboot配置文件中配置swagger.enable的布尔值即可
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Value("${swagger.enable}")
private boolean isEnableSwagger;
@Bean
public Docket docket(Environment environment) {
return new Docket(DocumentationType.SWAGGER_2).enable(isEnableSwagger);
}
通过注解@Profile()
@Configuration
@EnableSwagger2
@Profile({"dev", "test"})
public class SwaggerConfig { ... }
通过注解@ConditionalOnProperty()
不配置默认不启用Swagger,如需启用在相应的springboot配置文件中配置swagger.enable的布尔值即可
@Configuration
@EnableSwagger2
@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
public class SwaggerConfig { ... }
通过Environment判断
@Bean
public Docket docket(Environment environment) {
// 判断springboot配置环境是否是dev或test
boolean isEnableSwagger = environment.acceptsProfiles(Profiles.of("dev", "test"));
return new Docket(DocumentationType.SWAGGER_2).enable(isEnableSwagger);
}