webservice在进行run maven build中出现java.lang.ClassCastException错误
错误如下:
八月 29, 2025 3:39:00 下午 org.apache.coyote.AbstractProtocol init 信息: Initializing ProtocolHandler ["http-bio-8080"] 八月 29, 2025 3:39:00 下午 org.apache.catalina.core.StandardService startInternal 信息: Starting service Tomcat 八月 29, 2025 3:39:00 下午 org.apache.catalina.core.StandardEngine startInternal 信息: Starting Servlet Engine: Apache Tomcat/7.0.47 八月 29, 2025 3:39:00 下午 org.apache.catalina.core.ContainerBase startInternal 严重: A child container failed during start java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Tomcat].StandardHost[localhost].StandardContext[]] at 严重: A child container failed during start java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Tomcat].StandardHost[localhost]] at org.apache.maven.plugin.DefaultMojosExecutionStrategy.execute(DefaultMojosExecutionStrategy.java:39) at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:159) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:105) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:73) at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:53) at java.lang.Thread.run(Thread.java:748) Caused by: org.apache.catalina.LifecycleException: A child container failed during start at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1131) at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:800) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) ... 6 more [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.502 s [INFO] Finished at: 2025-08-29T15:39:00+08:00 [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.apache.tomcat.maven:tomcat7-maven-plugin:2.2:run (default-cli) on project 03_jaxwx_spring_server: Could not start Tomcat: Failed to start component [StandardServer[-1]]: Failed to start component [StandardService[Tomcat]]: Failed to start component [StandardEngine[Tomcat]]: A child container failed during start -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
错误大概是这种:Spring 的 SpringServletContainerInitializer
和 javax.servlet.ServletContainerInitializer
来自不同的类加载器(或 jar 包),通常是因为你的 classpath 中存在多个版本的 javax.servlet
API,最常见的是引入了 geronimo-servlet_3.0_spec
(通过某些依赖传递引入),或者与 Tomcat 提供的 Servlet API 冲突。
这里可以用:mvn dependency:tree去查看依赖:
然后去删掉冲突依赖,我这里是因为引入了geronimo-servlet_3.0_spec
它是 Apache Geronimo 项目提供的一个 Servlet 3.0 API 的实现,相当于是一个第三方的 javax.servlet
包。它 不是 Tomcat 自带的 Servlet API,也不是标准的 javax.servlet:javax.servlet-api
这个是由于这个依赖<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>${cxf.version}</version> <!-- 即 3.0.1 -->
</dependency>引起,这里我把这个依赖删掉正常启动起来了。