Vaadin+Spring Boot替换Tomcat为Undertow遇ERR_CONNECTION_CLOSED问题
Vaadin+Spring Boot替换Tomcat为Undertow后连接关闭问题解决
问题描述
将Vaadin+Spring Boot项目中的Tomcat替换为Undertow后,控制台无报错信息,但访问REST API或Vaadin视图时,浏览器仅提示“ERR_CONNECTION_CLOSED”错误,切回Tomcat则一切正常。
原build.gradle内容
configurations.all { exclude group: 'org.springframework.boot', module:'spring-boot-starter-tomcat' } dependencies { implementation "com.vaadin:vaadin-core:$vaadinVersion" implementation "org.springframework.boot:spring-boot-starter-security:$springBootVersion" implementation("org.springframework.boot:spring-boot-starter-web:$springBootVersion") { exclude group: 'com.fasterxml.jackson.core' exclude group: 'com.fasterxml.jackson.datatype' exclude group: 'com.fasterxml.jackson.module' exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat' exclude module: 'spring-boot-starter-tomcat' exclude group: 'org.apache.tomcat.embed' } implementation("org.springframework.boot:spring-boot-starter-undertow:$springBootVersion") implementation ("org.springframework.boot:spring-boot-starter-parent:$springBootVersion") { // exclude module: 'spring-boot-starter-tomcat' } implementation "org.springframework.boot:spring-boot-starter-mail:$springBootVersion" implementation "org.springframework.boot:spring-boot-configuration-processor:$springBootVersion" implementation("com.vaadin:vaadin-spring-boot-starter:$vaadinVersion") implementation ("com.vaadin:vaadin-spring-boot-starter:$vaadinVersion") implementation ("com.vaadin:vaadin-bom:${vaadinVersion}") { exclude group: 'org.springframework.boot', module:'spring-boot-starter-tomcat' } implementation 'com.google.code.gson:gson:2.10.1' implementation "org.springframework.boot:spring-boot-starter-data-jpa:$springBootVersion" }
核心问题与修复步骤
1. 修正依赖配置中的错误
原配置存在多处不合理的依赖声明,这是导致连接失败的核心原因:
- 错误将
spring-boot-starter-parent作为implementation依赖:parent是用来管理项目依赖版本和插件的,应在build.gradle顶部声明,而非作为业务依赖导入 - 重复添加
vaadin-spring-boot-starter:重复依赖可能导致版本冲突 - 错误导入
vaadin-bom:BOM是版本管理平台,需用platform方式导入,而非implementation - 过度排除Jackson依赖:Jackson是Spring Web的核心依赖,无特殊需求不应排除,否则会导致请求处理失败
2. 修正后的build.gradle示例
plugins { id 'org.springframework.boot' version "${springBootVersion}" id 'io.spring.dependency-management' version '1.1.4' id 'com.vaadin' version "${vaadinVersion}" } // 正确声明Spring Boot Parent parent { group 'org.springframework.boot' name 'spring-boot-starter-parent' version "${springBootVersion}" } // 全局排除Tomcat依赖 configurations.all { exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat' } dependencies { // 用platform导入Vaadin BOM管理版本 implementation platform("com.vaadin:vaadin-bom:${vaadinVersion}") implementation "com.vaadin:vaadin-core" implementation "com.vaadin:vaadin-spring-boot-starter" implementation "org.springframework.boot:spring-boot-starter-security" // 仅排除Web Starter中的Tomcat依赖 implementation("org.springframework.boot:spring-boot-starter-web") { exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat' } // 导入Undertow Starter implementation "org.springframework.boot:spring-boot-starter-undertow" implementation "org.springframework.boot:spring-boot-starter-mail" implementation "org.springframework.boot:spring-boot-configuration-processor" implementation 'com.google.code.gson:gson:2.10.1' implementation "org.springframework.boot:spring-boot-starter-data-jpa" }
3. 额外排查步骤
如果修正依赖后问题仍存在,检查以下内容:
- 确认Undertow启动状态:启动后查看控制台,是否有类似
Undertow started on port(s) 8080 (http)的日志,确认服务正常监听端口 - 端口占用检查:用
netstat -ano | findstr :8080(Windows)或lsof -i :8080(Linux/Mac)检查端口是否被其他程序占用 - SSL配置验证:如果项目启用HTTPS,确保
application.properties中的Undertow SSL配置正确,包括密钥存储路径、密码等 - Spring Security放行规则:确保Security配置中放行Vaadin的核心路径,比如
/VAADIN/**、/frontend/**、/login、/logout等,避免拦截正常请求 - Vaadin Push配置:若使用Push功能,确认Undertow的WebSocket支持正常(spring-boot-starter-undertow已默认包含WebSocket模块)
内容的提问来源于stack exchange,提问作者Jason The Dynamite
相关产品推荐
相关产品推荐

