@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();
}
}
9条答案
按热度按时间gfttwv5a1#
将您的swagger配置放到单独的配置类中,并使用
@Profile
annotation -〉对其进行注解,这样它将仅在某些配置文件中被扫描到Spring上下文中。示例:
然后,您可以通过命令行定义Sping Boot 应用运行的配置文件:
--spring.profiles.active=dev
或通过配置文件:spring.profiles.active=dev
.Read this section of Spring Boot docs for more info about
@Profile
6xfqseft2#
如果您在多个环境中工作,则还可以使用**@Profile**作为数组
wixjitnu3#
在swagger 3.0.0版本中,您可以在相应的环境配置文件
application.properties
文件中添加springfox.documentation.enabled=false
。例如,我将其添加到application-prod.properties
中,以便在生产中禁用(在运行应用程序时,您必须使用虚拟机参数(如-Dspring.profiles.active=prod
)指定配置文件)e0uiprwp4#
这是我的配置类:
每当我需要Swagger时,我都会将配置文件
swagger
添加到环境变量SPRING_PROFILES_ACTIVE
中fae0ux8s5#
除了使用 profile 配置Spring的答案之外,考虑在反向HTTP代理上设置规则,以阻止从LAN外部访问Swagger端点。这将为您提供一些针对Swagger端点攻击的深度防御。
okxuctiv6#
对于那些使用代码生成器(生成Swagger 2SpringBoot)的用户:
1.编写您自己的Swagger 2SpringBoot(带有@Profile位),并将其定位在与自动生成的包相同的包路径中。
1.编辑swagger-codegen-maven-plugin,将生成的代码放到src/main/java中(这将覆盖第1点中您自己的代码)。
1.编辑.swagger-codegen-ignore以避免覆盖您的Swagger 2SpringBoot
1.注意其他的东西也会被覆盖,比如pom.xml和application.properties。只要把它们也添加到.swagger-codegen-ignore中就可以了。
好了,好了
gywdnpxw7#
1.具有env配置
@Configuration
@EnableSwagger2启用 swagger
@Configuration 文件(“devone”)
MY_ENV您将从文件中读取,如.env
.env文件内容:MY_ENV=生产
在生产中保留的其他.env文件仅用于生产凭证。
tyky79it8#
一个老问题,但是如果你使用的是SpringDoc v1.2.12+:
来源:https://github.com/springdoc/springdoc-openapi/issues/191#issuecomment-558809236
mklgxw1f9#
**spring.profiles.active=production with @Profile(“!production”)**为我关闭了产品中的昂首阔步而工作。
例如:-