我想在我的 Spring Boot 应用程序投入生产时关闭 Swagger。我宁愿不使用 配置文件,因为我想要更细粒度的控制。
这是我的 Swagger 配置类和我添加的 @ConditionalOnProperty:
@Configuration
@ConditionalOnProperty(
value="module.enabled",
havingValue = "true",
matchIfMissing = false)
public class SpringFoxConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.OAS_30)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
应用程序配置文件具有以下值:
//The parameters visible in the Swagger UI
[email protected]@
[email protected]@
当我使用命令行重新启动我的应用程序时,
./mvnw spring-boot:运行
当我访问 http://localhost:8080/swagger-ui/index.html 时,我仍然可以看到 Swagger UI
我尝试将 SpringFoxConfig 文件更改为:
@Configuration
@Profile("dev")
public class SpringFoxConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.OAS_30)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
我使用命令行启动了 Spring Boot 应用程序:
./mvnw spring-boot:run -Dspring.profiles.active=prod
当应用程序启动时,我仍然可以看到 Swagger UI 我去了http://localhost:8080/swagger-ui/index.html
这是我的应用程序的 POM 文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<artifactId>SpringSandal</artifactId>
<build>
<plugins>
<plugin>
<artifactId>spring-boot-maven-plugin</artifactId>
<groupId>org.springframework.boot</groupId>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<artifactId>spring-boot-starter-web</artifactId>
<groupId>org.springframework.boot</groupId>
</dependency>
<dependency>
<artifactId>spring-boot-starter-actuator</artifactId>
<groupId>org.springframework.boot</groupId>
</dependency>
<!-- dependencies for Swagger and OpenAPI 3.0 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>
<!-- end of dependencies for Swagger -->
<dependency>
<artifactId>spring-boot-starter-test</artifactId>
<exclusions>
<exclusion>
<artifactId>junit-vintage-engine</artifactId>
<groupId>org.junit.vintage</groupId>
</exclusion>
</exclusions>
<groupId>org.springframework.boot</groupId>
<scope>test</scope>
</dependency>
</dependencies>
<description>Demo project for Spring Boot</description>
<groupId>com.example</groupId>
<modelVersion>4.0.0</modelVersion>
<name>SpringSandal</name>
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<relativePath/>
<version>2.3.2.RELEASE</version> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>11</java.version>
</properties>
<version>0.0.1-SNAPSHOT</version>
</project>
springdoc.swagger-ui.enabled=false springdoc.api-docs.enabled=false