【发布时间】:2021-05-07 17:10:23
【问题描述】:
我正在从 2.x 升级到 SpringFox Swagger 3.0.0,它引入了 Spring Boot 启动器 springfox-boot-starter 依赖项,从而消除了对基于 2.x 的 SwaggerConfig 的需求:
/**
* NO LONGER NEEDED
*/
@Configuration
@EnableSwagger2
@Profile({"local", "dev", "beta"}) // <- HOW TO DISABLE IN PROD INSTEAD OF THIS
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
现在我不再需要这个@Configuration,它允许我在@Profile 中指定我的环境配置文件并因此在生产中禁用Swagger,我如何在SpringFox Swagger-UI 3.x 中禁用生产中的Swagger?
注意:讨论了基于 Spring Security 的方法here,这对于某些人来说可能是一种选择,但对于这种情况不是一种选择,原因有两个:
- 我的应用程序没有使用 Spring Security,并且无法包含
spring-boot-security-starter依赖项 - 它需要将所有其他端点列入白名单才能让它们再次工作,这是不可接受的
【问题讨论】:
-
您可以尝试 @Profile({"!prod"}) 并在您的应用程序属性中添加 spring.profiles=prod springfox.documentation.enabled=false
-
感谢在下面发布参考答案后才看到这个。
标签: java spring-boot swagger swagger-ui springfox-boot-starter