Home > Software design >  java.lang.ClassCastException: class org.springframework.web.servlet.DispatcherServlet cannot be cast
java.lang.ClassCastException: class org.springframework.web.servlet.DispatcherServlet cannot be cast

Time:12-08

I've seen answers to questions similar to mine, but I've tried everything, the error doesn't go away. I understand that the api servlet classes are loaded by two different class loaders because there are multiple sources in the web deployment assembly. I tried to remove that with servlet-api-3.0-alpha-1.jar, but if I remove the same package from WEB-INF\lib folder, it gives me more errors. There is also to add that it is a spring project imported into eclipse, I had created a web dynamic project which I then converted into a maven project. Since by debugging, I discovered that the embedded server was not running the updated jsp pages for me, I changed the project to mvc so as to have the normal server in eclipse. The server is tomcat 10, I can't figure out which packages it loads from maven and which ones from the lib folder.

if I delete from lib servlet-api-3.0-alpha-1.jar, the error appears to me: The type javax.servlet.ServletContext cannot be resolved. It is indirectly referenced from required .class files MyInitializer.java

If I delete from lib jakarta.el-5.0-M1.jar, the error appears to me: The project cannot be built until build path errors are resolved

Project 'Parlamento' is missing required library: 'C:\Users\segreteria\Desktop\WORKSPACE 4\Parlamento\src\main\webapp\WEB-INF\lib\jakarta.el-5.0.0-M1.jar'

if I downgrade tomcat from 10 to 9, i have to change also the spring version because tomcat 9 doesn't support version 5, and then i have to change all the jar packages.

if i use tomcat 9.0.62 then i have to use spring 4 so i have to use applicationContext.xml which throws other errors which is fixed in later versions of tomcat and this brings me back to update spring packages again.

STACKTRACE:

SEVERE: Allocate exception for servlet [Parlamento]
java.lang.ClassCastException: class org.springframework.web.servlet.DispatcherServlet 
cannot be cast to class jakarta.servlet.Servlet 
(org.springframework.web.servlet.DispatcherServlet is in unnamed module of loader 
org.apache.catalina.loader.ParallelWebappClassLoader @1601e47; 
jakarta.servlet.Servlet is in unnamed module of loader java.net.URLClassLoader 
 @6bdf28bb)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1071)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:789)
at 
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:128)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
at 
org.apache.catalina.authenticator.AuthenticatorBase.invoke(
AuthenticatorBase.java:542)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:119)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at 
org.apache.catalina.valves.AbstractAccessLogValve.invoke
(AbstractAccessLogValve.java:690)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:356)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process
(AbstractProtocol.java:867)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun
(NioEndpoint.java:1762)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker
(ThreadPoolExecutor.java:1191)
at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run
(ThreadPoolExecutor.java:659)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:833)

web deployment assembly

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.5</version>
   <relativePath></relativePath>
</parent>
<groupId>com.giuggiola</groupId>
<artifactId>Parlamento</artifactId>

  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
<name>Parlamento</name>
<description>Demo project for Spring Boot</description>
<properties>
    <java.version>18</java.version>
</properties>

<dependencies>


<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>

<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-annotations-api</artifactId>
<!--   <version>10.0.23</version>-->
</dependency>


<dependency>
<groupId>jakarta.servlet.jsp.jstl</groupId>
 <artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
<!--  <version>2.0.0</version> -->
<scope>provided</scope>
</dependency>

<dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
      <!--    <version>1.2</version> -->
         <scope>provided</scope>
    </dependency>
   

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.1.5.Final</version>
<type>pom</type>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<!--   <version>6.0.0</version> -->
<scope>provided</scope>
</dependency>

 <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
 
    <scope>provided</scope>
</dependency>
 
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
 <!--     <version>4.13.2</version>-->
<scope>provided</scope>

</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
 <!-- <version>5.3.23</version> -->
   <scope>provided</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
    <groupId>jakarta.servlet</groupId>
    <artifactId>jakarta.servlet-api</artifactId> 
    <scope>provided</scope>
</dependency>

<dependency>
    <groupId>jakarta.servlet.jsp</groupId>
    <artifactId>jakarta.servlet.jsp-api</artifactId>
    <version>3.0.0</version>
    <scope>provided</scope>
</dependency>
 
<dependency>
    <groupId>jakarta.el</groupId>
    <artifactId>jakarta.el-api</artifactId>
    <version>4.0.0</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>jakarta.websocket</groupId>
    <artifactId>jakarta.websocket-api</artifactId>
 <!--  <version>2.0.0</version>  -->  
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>jakarta.security.enterprise</groupId>
    <artifactId>jakarta.security.enterprise-api</artifactId>
    <version>2.0.0</version>
    <scope>provided</scope>
</dependency>


<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<!--    <version>42.5.0</version> --> 
</dependency>

<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
 <!--  <version>2.7.3</version>-->
<scope>provided</scope>

</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<!--  <version>2.7.4</version>-->
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<!-- <version>2.7.4</version>-->
<scope>provided</scope>
</dependency>

<dependency>
        <groupId>com.vladmihalcea</groupId>
        <artifactId>hibernate-types-55</artifactId>
        <version>2.14.0</version>
    </dependency>

 </dependencies>

 <build>
 <finalName>Parlamento</finalName>


    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        
        
        <!-- plugin che ho aggiunto io-->
        <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<!--  <version>3.2.0</version> -->
<!--  <scope>provided</scope> -->
</plugin>    
    </plugins>

    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>

</build>
</project>

lib lib

STACKTRACE:

dic 06, 2022 6:13:12 PM 
org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version name:   Apache Tomcat/9.0.69
dic 06, 2022 6:13:12 PM 
org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built:          Nov 9 2022 18:43:47 UTC
dic 06, 2022 6:13:12 PM 
org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version number: 9.0.69.0
dic 06, 2022 6:13:12 PM 
org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name:               Windows 8
dic 06, 2022 6:13:12 PM 
org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version:            6.2
dic 06, 2022 6:13:12 PM 
org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture:          amd64
dic 06, 2022 6:13:12 PM 
org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home:             C:\Program Files\Java\jdk- 
17.0.5_windows-x64_bin\jdk-17.0.5
dic 06, 2022 6:13:12 PM 
org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version:           17.0.5 9-LTS-191
dic 06, 2022 6:13:12 PM 
org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor:            Oracle Corporation
dic 06, 2022 6:13:12 PM 
org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE:         
C:\Users\segreteria\Desktop\WORKSPACE 
4\.metadata\.plugins\org.eclipse.wst.server.core\tmp1
dic 06, 2022 6:13:12 PM 
org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME:         C:\Program Files\Apache Software 
Foundation\Tomcat 9.0_Tomcat9.0.69
dic 06, 2022 6:13:12 PM 
org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: - 
XX: ShowCodeDetailsInExceptionMessages
dic 06, 2022 6:13:12 PM 
org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: - 
agentlib:jdwp=transport=dt_socket,suspend=y,address=
localhost:65348
dic 06, 2022 6:13:12 PM 
org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: - 
javaagent:C:\Users\segreteria\eclipse\jee-2022- 
06\eclipse\configuration\org.eclipse.osgi\407\0\.cp\lib\
javaagent-shaded.jar
dic 06, 2022 6:13:12 PM 
org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: - 
Dcatalina.base=C:\Users\segreteria\Desktop\WORKSPACE 
4\.metadata\.plugins\org.eclipse.wst.server.core\tmp1
dic 06, 2022 6:13:12 PM 
org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=C:\Program 
Files\Apache Software Foundation\Tomcat 9.0_Tomcat9.0.69
dic 06, 2022 6:13:12 PM 
org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -  
.deploy=C:\Users\segreteria\Desktop\WORKSPACE 
4\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps
dic 06, 2022 6:13:12 PM 
org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: --add-opens=java.base/java.lang=ALL- 
UNNAMED
dic 06, 2022 6:13:12 PM 
org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: --add-opens=java.base/java.io=ALL- 
UNNAMED
dic 06, 2022 6:13:12 PM 
org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: --add-opens=java.base/java.util=ALL- 
UNNAMED
dic 06, 2022 6:13:12 PM 
org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: --add- 
opens=java.base/java.util.concurrent=ALL-UNNAMED
dic 06, 2022 6:13:12 PM 
org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: --add- 
opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
dic 06, 2022 6:13:12 PM 
org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=UTF-8
dic 06, 2022 6:13:13 PM 
org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The Apache Tomcat Native library which allows using OpenSSL 
was not found on the java.library.path: [C:\Program 
Files\Java\jdk-17.0.5_windows-x64_bin\jdk- 
17.0.5\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:
Windows;C:\Program Files\Common 
Files\Oracle\Java\javapath;C:\Program Files (x86)\Common 
Files\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;
C:\Windows\System32\Wbem;C:\Windows\System32\
WindowsPowerShell\v1.0\;C:\Maven\apache-maven-3.8.6- 
bin\bin;C:\Program Files\Java\jdk-17.0.5_windows-x64_bin\jdk- 
17.0.5\bin;;C:\Users\segreteria\AppData\Local\Programs\Microsoft 
VS Code\bin;C:\Program Files\Java\jdk-17.0.5_windows-x64_bin\jdk- 
17.0.5\bin;.]
dic 06, 2022 6:13:14 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8088"]
dic 06, 2022 6:13:15 PM org.apache.catalina.startup.Catalina load
INFO: Server initialization in [5346] milliseconds
dic 06, 2022 6:13:15 PM org.apache.catalina.core.StandardService 
startInternal
INFO: Starting service [Catalina]
dic 06, 2022 6:13:15 PM org.apache.catalina.core.StandardEngine 
startInternal
INFO: Starting Servlet engine: [Apache Tomcat/9.0.69]
dic 06, 2022 6:13:18 PM org.apache.jasper.servlet.TldScanner 
scanJars
INFO: At least one JAR was scanned for TLDs yet contained no 
TLDs. Enable debug logging for this logger for a complete list of 
JARs that were scanned but no TLDs were found in them. Skipping 
unneeded JARs during scanning can improve startup time and JSP 
compilation time.
dic 06, 2022 6:13:18 PM 
org.apache.catalina.util.SessionIdGeneratorBase 
createSecureRandom
WARNING: Creation of SecureRandom instance for session ID 
generation using [SHA1PRNG] took [122] milliseconds.
dic 06, 2022 6:13:26 PM org.apache.jasper.servlet.TldScanner 
scanJars
INFO: At least one JAR was scanned for TLDs yet contained no 
TLDs. Enable debug logging for this logger for a complete list of 
JARs that were scanned but no TLDs were found in them. Skipping 
unneeded JARs during scanning can improve startup time and JSP 
compilation time.

CodePudding user response:

Spring 5 doesn't support Tomcat 10 but it runs perfectly fine on Tomcat 9.

In fact I just tested it by creating a simple project with your slightly fixed POM and running it on Tomcat 9.0.69.

Here's the POM:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    
    <groupId>com.example</groupId>
    <artifactId>tomcat-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>tomcat-demo</name>
    <description>Demo project for Spring Boot</description>
    
    <properties>
        <java.version>17</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-annotations-api</artifactId>
            <!--   <version>10.0.23</version>-->
        </dependency>

        <dependency>
            <groupId>javax.servlet.jsp.jstl</groupId>
            <artifactId>jstl-api</artifactId>
            <version>1.2</version>
            <!--  <version>2.0.0</version> -->
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <!--    <version>1.2</version> -->
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.hibernate.orm</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>6.1.5.Final</version>
            <type>pom</type>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <!--   <version>6.0.0</version> -->
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>

            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <!--     <version>4.13.2</version>-->
            <scope>provided</scope>

        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <!-- <version>5.3.23</version> -->
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.el</groupId>
            <artifactId>el-api</artifactId>
            <version>2.2</version>
            <scope>provided</scope>
        </dependency>
        
        <dependency>
            <groupId>javax.websocket</groupId>
            <artifactId>javax.websocket-api</artifactId>
            <!--  <version>2.0.0</version>  -->
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.security.enterprise</groupId>
            <artifactId>javax.security.enterprise-api</artifactId>
            <version>1.0</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <!--    <version>42.5.0</version> -->
        </dependency>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <!--  <version>2.7.3</version>-->
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <!--  <version>2.7.4</version>-->
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot</artifactId>
            <!-- <version>2.7.4</version>-->
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>com.vladmihalcea</groupId>
            <artifactId>hibernate-types-55</artifactId>
            <version>2.14.0</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <!--  <version>3.2.0</version> -->
                <!--  <scope>provided</scope> -->
            </plugin>
        </plugins>

        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

</project>

Another thing to note is that there must be an implementation of SpringBootServletInitializer in your project to initialize the Servlet context required by Tomcat. You can either make your @SpringBootApplication (or any @Configuration) extend SpringBootServletInitializer or make it a separate class:

public class ServletInitializer extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(YourSpringBootApplication.class);
    }
}
  • Related