Home > Software design >  Can I use CXF with Tomcat 10?
Can I use CXF with Tomcat 10?

Time:10-03

I am getting the following error when using CXF with Tomcat 10:

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

I understand that Tomcat 10 supports Servlet 5.0 and uses the jakarta.servlet packages instead of javax.servlet that CXF uses. Does that mean that I can't use Tomcat 10 with CXF and should downgrade to Tomcat 9? I am using the latest cxf, 3.4.4. The code used to work when using Tomcat 5 and cxf 2.x.

The full stacktrace is:

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)

CodePudding user response:

There is no official version of CXF supporting Jakarta EE 9, however you can use the Tomcat Migration Tool for Jakarta EE to replace the namespace in the CXF jar files and your source code.

This is the approach also chosen by TomEE (which uses CXF) for its 9.0 series, so it should be relatively safe.

Tomcat 10.0 itself integrates the migration tool and can convert your WAR file before deployment if you put it in the webapps-javaee folder.

  • Related