學習目標
快速學會使用注解關閉Swagger2,避免接口重復暴露。
使用教程
禁用方法1:使用注解@Profile({"dev","test"})
表示在開發或測試環境開啟,而在生產關閉。(推薦使用)
禁用方法2:使用注解@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
然后在測試配置或者開發配置中 添加 swagger.enable = true 即可開啟,生產環境不填則默認關閉Swagger.
@Configuration
@EnableSwagger2
//@Profile({"dev","test"})
@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
public class Swagger2Config {
/**
* 添加摘要信息(Docket)
*/
@Bean
public Docket controllerApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(new ApiInfoBuilder()
.title("接口文檔")
.description("具體包括XXX,XXX模塊...")
.contact(new Contact("Socks", null, null))
.version("版本號:1.0")
.build())
.select()
.apis(RequestHandlerSelectors.basePackage("com.hehe.controller"))
.paths(PathSelectors.any())
.build();
}