You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

更新依赖后Tomcat9部署CXF Web服务遇类转换异常求助

类路径冲突排查方法
  • 执行Maven依赖树分析命令,定位所有Servlet API相关依赖:
    运行mvn dependency:tree -Dincludes=javax.servlet:javax.servlet-api,jakarta.servlet:jakarta.servlet-api,该命令会列出项目中直接或间接引入的所有Servlet API依赖,重点查看是否存在多版本、非provided范围的依赖。
  • 检查WAR包内容:
    解压打包生成的WAR文件,查看WEB-INF/lib目录下是否包含servlet-api*.jar或jakarta.servlet-api*.jar——Tomcat自带Servlet API,项目打包时不应将该类库打进WAR,否则会触发类加载冲突。
  • 排查CXF传递依赖:
    CXF的cxf-rt-transports-http-servlet等核心依赖可能间接引入Servlet API,需确认这些传递依赖的范围是否合理。
冲突解决方案
  • 明确声明Servlet API依赖并设置provided范围:
    在pom.xml中添加如下配置,指定与Tomcat 9匹配的Servlet 4.0版本,并标记为容器提供(无需打包):
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.1</version>
        <scope>provided</scope>
    </dependency>
    
  • 排除CXF依赖中传递的Servlet API:
    若CXF的依赖间接引入了未设置provided的Servlet API,在pom.xml中添加排除规则:
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-transports-http-servlet</artifactId>
        <version>你的CXF实际版本</version>
        <exclusions>
            <exclusion>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    
  • 移除Jakarta版本的Servlet API依赖:
    Tomcat 9使用的是javax.servlet包的Servlet API,若项目中引入了jakarta.servlet:jakarta.servlet-api,需立即移除,替换为上述javax版本的依赖。
  • 校验web.xml配置:
    确保web.xml使用与Tomcat 9匹配的4.0版本规范,且CXFServlet类路径无拼写错误:
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
             version="4.0">
        <servlet>
            <servlet-name>CXFServlet</servlet-name>
            <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        </servlet>
        <!-- 其他servlet映射等配置 -->
    </web-app>
    

内容的提问来源于stack exchange,提问作者Francesco

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 03:28:21