Spring Boot 2响应式项目启动时未用Netty,默认启动Tomcat问题
Hey there! I’ve run into this exact snag before—let’s break down what’s going on and how to fix it.
The Root Cause
Spring Boot’s auto-configuration picks a server based on what’s in your classpath:
- When you use
spring-boot-starter-webflux, Netty is the default reactive server out of the box. - But if any Servlet-stack dependencies sneak into your classpath (like
spring-boot-starter-web), Spring Boot will automatically switch to a Servlet container (Tomcat, by default) instead of Netty.
Looking at your dependencies, the culprit is almost certainly spring-boot-starter-hateoas. In Spring Boot 2.x, this starter automatically pulls in spring-boot-starter-web (the traditional Servlet stack) unless you explicitly exclude it. That’s why Tomcat is firing up instead of Netty.
Fix Steps
Exclude the Servlet stack from the HATEOAS starter
Modify yourspring-boot-starter-hateoasdependency inbuild.gradleto exclude the unwantedspring-boot-starter-webmodule:compile("org.springframework.boot:spring-boot-starter-hateoas:${springBootVersion}") { exclude group: 'org.springframework.boot', module: 'spring-boot-starter-web' }Verify your dependency tree
Run this command to double-check that no Servlet-related dependencies are still lurking in your project:./gradlew dependenciesLook for entries like
org.springframework.boot:spring-boot-starter-tomcat—if you still see them, trace back to which starter is bringing them in and repeat the exclusion step.Rebuild and restart
After making the changes, rebuild your project and start the app. It should now use Netty as the default server for your reactive WebFlux application.
Quick Note
If you’re using any other starters (like spring-boot-starter-data-jdbc or similar), make sure none of them are pulling in spring-boot-starter-web accidentally. The dependency tree check will help you spot those hidden dependencies.
内容的提问来源于stack exchange,提问作者Dina Bogdan

