Spring Boot启动报错:ExecutionException与Tomcat组件启动失败求助
解决
java.lang.NoSuchMethodError: javax.servlet.ServletContext.getVirtualServerName()异常 环境信息
- Java 1.8
- Spring Boot v2.7.5
问题现象
执行mvn install编译成功,但运行程序时抛出运行时异常,最初怀疑Tomcat重复启动导致,排查日志后确认根源异常为:
java.lang.NoSuchMethodError: javax.servlet.ServletContext.getVirtualServerName()Ljava/lang/String;
完整运行日志:
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.7.5) Dec 29, 2022 12:15:41 PM org.apache.coyote.AbstractProtocol init INFO: Initializing ProtocolHandler ["https-jsse-nio-8443"] Dec 29, 2022 12:15:41 PM org.apache.catalina.core.StandardService startInternal INFO: Starting service [Tomcat] Dec 29, 2022 12:15:41 PM org.apache.catalina.core.StandardEngine startInternal INFO: Starting Servlet engine: [Apache Tomcat/9.0.68] Dec 29, 2022 12:15:41 PM org.apache.catalina.core.ContainerBase startInternal SEVERE: A child container failed during start java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [NonLoginAuthenticator[StandardEngine[Tomcat].StandardHost[localhost].TomcatEmbeddedContext[]]] at java.util.concurrent.FutureTask.report(FutureTask.java:122) at java.util.concurrent.FutureTask.get(FutureTask.java:192) Caused by: org.apache.catalina.LifecycleException: Failed to start component [NonLoginAuthenticator[StandardEngine[Tomcat].StandardHost[localhost].TomcatEmbeddedContext[]]] at org.apache.catalina.util.LifecycleBase.handleSubClassException(LifecycleBase.java:440) Dec 29, 2022 12:15:41 PM org.apache.catalina.core.ContainerBase startInternal SEVERE: A child container failed during start java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: A child container failed during start at java.util.concurrent.FutureTask.report(FutureTask.java:122) at java.util.concurrent.FutureTask.get(FutureTask.java:192) ... 21 more Caused by: java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [NonLoginAuthenticator[StandardEngine[Tomcat].StandardHost[localhost].TomcatEmbeddedContext[]]] ... 29 more Caused by: org.apache.catalina.LifecycleException: Failed to start component [NonLoginAuthenticator[StandardEngine[Tomcat].StandardHost[localhost].TomcatEmbeddedContext[]]] ... 29 more Caused by: java.lang.NoSuchMethodError: javax.servlet.ServletContext.getVirtualServerName()Ljava/lang/String; ... 39 more Dec 29, 2022 12:15:41 PM org.apache.coyote.AbstractProtocol pause INFO: Pausing ProtocolHandler ["https-jsse-nio-8443"] Dec 29, 2022 12:15:41 PM org.apache.catalina.core.StandardService stopInternal INFO: Stopping service [Tomcat] Dec 29, 2022 12:15:41 PM org.apache.coyote.AbstractProtocol destroy INFO: Destroying ProtocolHandler ["https-jsse-nio-8443"] Process finished with exit code 1
解决方案
该异常核心原因是Servlet API版本不兼容:getVirtualServerName()是Servlet 3.1规范新增的接口,项目中可能引入了Servlet 3.0及以下版本的依赖,与Spring Boot内嵌Tomcat 9.0.x(依赖Servlet 4.0规范)产生冲突。
具体操作步骤:
排查依赖冲突
执行mvn dependency:tree命令,查找所有javax.servlet:javax.servlet-api或javax.servlet:servlet-api依赖,确认是否存在低版本依赖被引入。排除低版本Servlet依赖
在pom.xml中找到引入低版本Servlet API的依赖,添加<exclusions>标签排除该依赖。示例:<dependency> <groupId>依赖组ID</groupId> <artifactId>依赖artifact ID</artifactId> <version>版本号</version> <exclusions> <exclusion> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> </exclusion> </exclusions> </dependency>强制指定Servlet API版本
若无法定位冲突,可在pom.xml中显式声明Servlet API版本(Spring Boot 2.7.x对应版本为4.0.1):<dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency>设置
<scope>provided</scope>避免与内嵌Tomcat的Servlet API打包冲突。清理重构项目
执行mvn clean install清理旧依赖包,重新构建后运行程序。
内容的提问来源于stack exchange,提问作者user13106846
相关产品推荐
相关产品推荐

