禁用方法1:
使用注解@Profile({“dev”,“test”}) 表示在开发或测试环境开启,而在生产关闭。(推荐使用)
@Configuration
@EnableSwagger2
@EnableKnife4j
@Profile({"dev", "test"}) // 只允许在开发测试服务器访问Swagger
public class SwaggerConfiguration implements WebMvcConfigurer {
/**
* 注入资源文件
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("doc.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars*")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
@Bean
public Docket docketApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(getApiInfo())
.useDefaultResponseMessages(false)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
/**
* API详情配置
*/
private ApiInfo getApiInfo() {
return new ApiInfoBuilder()
.title("Platform接口文档")
.description("<div style='font-size:14px;color:red;'>knife4j swagger RESTful APIs</div>")
.version("1.0")
.build();
}
}
禁用方法2:使用注解
@ConditionalOnProperty(name = “swagger.enable”, havingValue = “true”)
然后在测试配置或者开发配置中 添加 swagger.enable = true 即可开启, 生产环境不填则默认关闭Swagger。