我可以将 CXF 与 Tomcat 10 一起使用吗?

Can I use CXF with Tomcat 10?

将 CXF 与 Tomcat 10 一起使用时出现以下错误:

java.lang.ClassCastExce
ption: class org.apache.cxf.transport.servlet.CXFServlet cannot be cast to class jakarta.servlet.Servlet

我知道 Tomcat 10 支持 Servlet 5.0 并使用 jakarta.servlet 包而不是 CXF 使用的 javax.servlet。这是否意味着我不能将 Tomcat 10 与 CXF 一起使用并且应该降级到 Tomcat 9?我使用的是最新的 cxf,3.4.4。使用 Tomcat 5 和 cxf 2.x.

时使用的代码

完整的堆栈跟踪是:

java.lang.ClassCastException: class org.apache.cxf.transport.servlet.CXFServlet cannot be cast to class jakarta.servlet.Servlet (org.apache.cxf.transport.servlet.CXFServlet is in unnamed module of loader org.apache.catalina.loader.ParallelWebappClassLoader @3b2c6f60; jakarta.servlet.Servlet is in unnamed module of loader java.net.URLClassLoader @77a567e1)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1050)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:990)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4906)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5213)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:728)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:700)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:747)
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:1027)
    at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:2001)
    at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
    at java.base/java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:123)
    at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:828)
    at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:478)
    at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1757)
    at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:316)
    at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:123)
    at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1164)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1367)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1371)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1349)
    at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
    at java.base/java.util.concurrent.FutureTask.runAndReset(FutureTask.java:305)
    at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:305)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.base/java.lang.Thread.run(Thread.java:831)

没有支持 Jakarta EE 9 的官方 CXF 版本,但是您可以使用 Tomcat Migration Tool for Jakarta EE 替换 CXF jar 文件和源代码中的命名空间。

9.0系列TomEE(使用CXF)也是采用这种方式,所以应该是比较安全的。

Tomcat 10.0 本身集成了迁移工具,如果您将 WAR 文件放在 webapps-javaee 文件夹中,可以在部署前转换它。