问题描述
我想在我的 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
application-description=@project.description@
application-version=@project.version@
当我使用命令行重新启动应用程序时,
./mvnw spring-boot:run
当我访问 http://localhost:8080/swagger-ui/index.html 时仍然可以看到 Swagger UI
@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>
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)