我有一个 Spring Boot Web 应用程序,它应该使用默认的反应式客户端 Netty 作为反应式应用程序运行,但是我需要包含几个 Maven 依赖项,这些依赖项将
sprint-boot-starter-web
放在类路径上。我知道这会导致 Spring 默认使用 Tomcat 而不是 Netty 的 servlet 样式应用程序,但根据 documentation 和 other StackOverflow posts,我应该能够通过执行以下任何/所有操作来规避此行为:
但是,这些选项都不起作用。我已经尝试以各种可以想象的方式设置应用程序类型,但应用程序仍然以 Tomcat 启动,除非我明确地将 spring-starter-web 从包含它的每个依赖项中排除,这使我的 pom.xml 变得一团糟。
在资源/application.yml中:
spring:
main:
web-application-type: REACTIVE
...不起作用。
在Application.java中:
@EnableConfigurationProperties
@EnableWebFlux
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(Application.class);
app.setWebApplicationType(WebApplicationType.REACTIVE);
app.run();
...也不起作用。
如何强制我的应用程序使用默认 Netty 客户端作为反应式 WebFlux 应用程序运行,而不需要繁琐地排除所有
spring-starter-web
依赖项?在 Java 17 上运行 Spring Boot 3.2.2。
从 Spring Boot 2.0 版本开始,“spring-boot-starter-web”依赖项包括 Servlet 和 Reactive 堆栈。如果您想确保您的应用程序仅使用 Netty 与 Reactive 堆栈一起运行,您可以排除与 servlet 相关的依赖项。您可以使用以下几个依赖项和 main 方法作为问题的解决方案。
pom.xml;
<dependencies>
<!-- Exclude the servlet-related dependencies from spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
<!-- Exclude any other servlet-related dependencies if needed -->
</exclusions>
</dependency>
<!-- Use spring-boot-starter-webflux with Netty as the reactive server -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
</dependencies>
主要方法;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.reactive.config.EnableWebFlux;
@EnableWebFlux
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}