spring java.lang.ClassCastException:无法将类强制转换为类jakarta.servlet.Servlet

j9per5c4  于 2022-12-02  发布在  Spring
关注(0)|答案(1)|浏览(246)

bounty将在6天后过期。回答此问题可获得+50的声望奖励。utente1234567890正在寻找来自知名来源的答案:如果我使用tomcat 9.0.62,那么我必须使用spring 4,所以我必须使用applicationContext.xml,这会引发其他错误,这在tomcat的后续版本中得到修复,这会让我再次更新spring包,有人能回答我吗?

我看到过类似问题的答案,但是我尝试了所有方法,错误仍然没有消失。我知道api servlet类是由两个不同的类加载器加载的,因为在web部署程序集中有多个源代码。我试图用servlet-api-3.0-alpha-1.jar删除它,但是如果我从WEB-INF\lib文件夹中删除同一个包,它给了我更多的错误。还有一点需要补充的是,它是一个导入到Eclipse中的Spring项目,我创建了一个Web动态项目,然后将其转换为Maven项目。由于通过调试,我发现嵌入式服务器没有为我运行更新的JSP页面,我把这个项目改成了mvc,以便在eclipse中有一个正常的服务器。服务器是tomcat 10,我不知道它从maven加载哪些包,从lib文件夹加载哪些包。
如果我从lib servlet-api-3.0-alpha-1.jar中删除,则会出现以下错误:无法解析类型javax.servlet.ServletContext。它是从必需的.class文件www.example.com间接引用MyInitializer.java
如果我从lib jakarta.el-5.0-M1.jar中删除,则会出现以下错误:在解决生成路径错误之前无法生成项目
项目“Parlamento”缺少所需的库:'C:\用户\隔离\桌面\工作空间4\议会\源文件\主文件\网络应用程序\网络信息\库\jakarta.el-5.0.0-M1.jar'
如果我把tomcat从10降级到9,我还必须更改spring版本,因为tomcat 9不支持版本5,然后我必须更改所有的jar包。
如果我使用Tomcat 9.0.62,那么我必须使用Spring 4,所以我必须使用applicationContext.xml,这会引发其他错误,这些错误在Tomcat的后续版本中得到修复,这会让我再次更新Spring包。
堆栈跟踪:

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)

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>


kmpatx3s

kmpatx3s1#

Spring 5不支持Tomcat 10,但它在Tomcat 9上运行得非常好。
事实上,我只是用稍微修复的POM创建了一个简单的项目,并在Tomcat 9.0.69上运行了它。
以下是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>

另一件需要注意的事情是,在您的项目中必须有SpringBootServletInitializer的实现,以初始化Tomcat所需的Servlet上下文。您可以让您的@SpringBootApplication(或任何@Configuration)扩展SpringBootServletInitializer,或者让它成为一个单独的类:

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

相关问题