Java 你如何在生产中关闭 swagger-ui
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37794571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you turn off swagger-ui in production
提问by user301693
I have swagger plugged in to my spring boot application. Spring boot allows you to have property files for each environment that you have. Is there a way to disable swagger for a production environment?
我已经大摇大摆地插入了我的 Spring Boot 应用程序。Spring Boot 允许您为您拥有的每个环境拥有属性文件。有没有办法为生产环境禁用 swagger?
回答by luboskrnac
Put your swagger configuration into separate configuration class and annotate it with @Profile
annotation -> so that it will be scanned into Spring context only in certain profiles.
将您的 swagger 配置放入单独的配置类中,并使用@Profile
annotation ->对其进行注释,以便仅在某些配置文件中将其扫描到 Spring 上下文中。
Example:
例子:
@Configuration
@EnableSwagger2
@Profile("dev")
public class SwaggerConfig {
// your swagger configuration
}
You can than define profile your Spring Boot app is operating in via command line: --spring.profiles.active=dev
or via config file: spring.profiles.active=dev
.
您可以通过命令行定义您的 Spring Boot 应用程序正在运行的--spring.profiles.active=dev
配置文件:或通过配置文件:spring.profiles.active=dev
。
Read this section of Spring Boot docs for more info about @Profile
回答by user3105453
This is my configuration class:
这是我的配置类:
@Configuration
@Profile("swagger")
@EnableSwagger2
public class SwaggerConfig {
@Value("${info.build.version}")
private String buildVersion;
@Bean
public Docket documentation() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(regex("/rest/.*"))
.build()
.pathMapping("/")
.apiInfo(metadata());
}
private ApiInfo metadata() {
return new ApiInfoBuilder()
.title("API documentation of our App")
.description("Use this documentation as a reference how to interact with app's API")
.version(buildVersion)
.contact(new Contact("Dev-Team", "https://dev-website", "dev@mailbox"))
.build();
}
}
Wherever I need Swagger, I add the profile swagger
to the environment variable SPRING_PROFILES_ACTIVE
无论我在哪里需要 Swagger,我都会将配置文件添加swagger
到环境变量中 SPRING_PROFILES_ACTIVE
回答by Pervez
If you are working on multiple environments then you can also use @Profileas array
如果您在多个环境中工作,那么您也可以使用@Profile作为数组
@Configuration
@EnableSwagger2
@Profile({"dev","qa"})
public class SwaggerConfig {
// your swagger configuration
}
回答by Jacques Koorts
For those that use code gen (which generates Swagger2SpringBoot):
对于那些使用代码生成(生成 Swagger2SpringBoot)的人:
- Write your own Swagger2SpringBoot (with the @Profile bit) and locate it in the same package path as the autogenerated one.
- Edit swagger-codegen-maven-plugin to place generated into src/main/java (which will overwrite your own one in point 1.
- Edit .swagger-codegen-ignore to not overwrite your Swagger2SpringBoot
- Note other stuff will also be overwritten eg. pom.xml and application.properties. Just add them to .swagger-codegen-ignore too.
- 编写您自己的 Swagger2SpringBoot(使用 @Profile 位)并将其定位在与自动生成的包路径相同的包路径中。
- 编辑 swagger-codegen-maven-plugin 将生成的放入 src/main/java (这将覆盖您自己的第 1 点。
- 编辑 .swagger-codegen-ignore 以不覆盖您的 Swagger2SpringBoot
- 注意其他东西也将被覆盖,例如。pom.xml 和 application.properties。也只需将它们添加到 .swagger-codegen-ignore 即可。
Done.
完毕。
回答by Armen Arzumanyan
have configuration for env
@Configuration
@EnableSwagger2
@Profile("devone")
application.yaml
profiles: active: ${MY_ENV:devone}
有 env 的配置
@配置
@EnableSwagger2
@Profile("devone")
应用程序.yaml
profiles: active: ${MY_ENV:devone}
MY_ENV you will read from file, like .env
MY_ENV 您将从文件中读取,例如 .env
.env file content: MY_ENV=prod
.env 文件内容:MY_ENV=prod
In the production keep other .env file only for production credentials.
在生产中保留其他 .env 文件仅用于生产凭据。