Spring Boot响应式应用默认启动Tomcat而非Netty问题排查
Hey there! Let's break down why your WebFlux app is spinning up Tomcat instead of the expected Netty server, and how to fix it.
The Root Cause
The issue traces back to your spring-boot-starter-data-rest dependency. Under the hood, this starter automatically pulls in spring-boot-starter-web — the core dependency for Spring MVC, the traditional Servlet-based stack.
Spring Boot's auto-configuration logic has a key priority rule here: if both Spring MVC and Spring WebFlux dependencies are present on the classpath, it defaults to enabling the Servlet stack (Tomcat). Even with @EnableWebFlux added to your main class, this gets overridden because Spring Boot assumes you want to use the Servlet environment when both stacks are available.
How to Fix It
You need to exclude the unwanted spring-boot-starter-web dependency from spring-boot-starter-data-rest so only the WebFlux stack remains. Here's how to update your pom.xml:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-rest</artifactId> <exclusions> <!-- Exclude Spring MVC dependency to force WebFlux + Netty --> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </exclusion> </exclusions> </dependency>
Bonus Tip
You can actually remove the @EnableWebFlux annotation from your main class. When no Spring MVC dependency is present, Spring Boot's auto-configuration will automatically set up the WebFlux environment with Netty. Adding @EnableWebFlux disables some of these default auto-configurations, which you likely don't need unless you're customizing WebFlux settings explicitly.
After making these changes, restart your app. You should see log output confirming Netty has started on port 8080 instead of Tomcat.
内容的提问来源于stack exchange,提问作者Bilbo Baggins

