使用Spring MVC禁用Swagger
问题内容:
我在Spring MVC中使用Swagger。我想在特定环境(例如生产环境)中有选择地禁用摇摇欲坠。我怎样才能做到这一点?
问题答案:
如果您使用的是1.x版本的springfox,以前是swagger-springmvc
当您配置swagger spring-mvc插件时,您可以使用enable
可以根据环境/配置文件等传入布尔值的方法。
@Bean
public SwaggerSpringMvcPlugin customImplementation(){
return new SwaggerSpringMvcPlugin(this.springSwaggerConfig)
.apiInfo(apiInfo())
.enable(environmentSpeficicBooleanFlag) //<--- Flag to enable or disable possibly loaded using a property file
.includePatterns(".*pet.*");
}
另一种方法是使用弹簧轮廓
@Bean
@Profile("production")
public SwaggerSpringMvcPlugin customImplementation(){
return new SwaggerSpringMvcPlugin(this.springSwaggerConfig)
.apiInfo(apiInfo())
.enable(false) //<--- Flag set to false in the production profile
.includePatterns(".*pet.*");
}
如果您使用的是2.x版本的springfox
当您配置swagger spring-mvc插件时,您可以使用enable
可以根据环境/配置文件等传入布尔值的方法。
@Bean
public Docket customImplementation(){
return new Docket(SWAGGER_2)
.apiInfo(apiInfo())
.enable(environmentSpeficicBooleanFlag) //<--- Flag to enable or disable possibly loaded using a property file
.includePatterns(".*pet.*");
}
另一种方法是使用弹簧轮廓
@Bean
@Profile("production")
public Docket customImplementation(){
return new Docket(SWAGGER_2)
.apiInfo(apiInfo())
.enable(false) //<--- Flag set to false in the production profile
.includePatterns(".*pet.*");
}