SpringServletContainerInitializer cannot be cast to javax.servlet.ServletContainerInitializer

后端 未结 6 1436
广开言路
广开言路 2020-12-03 11:09

I am attempting to move a xml-based Spring MVC app to a Java Configuration based app. There appears to be a mismatch with the various java.servlet classes available in mave

相关标签:
6条回答
  • 2020-12-03 11:19

    I had a similar problem without Spring, where mvn tomcat7:run did not work, despite the servlet deploying to OpenShift just fine. As per Biju Kunjummen's answer, the problem for me was which servlet-api I was compiling against.

    Broken:

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

    Fixed:

    <dependency>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>servlet-api</artifactId>
        <version>3.0.20100224</version>
        <scope>provided</scope>
    </dependency>
    

    Not terribly sure why, but it works now.

    0 讨论(0)
  • 2020-12-03 11:21

    I was facing the same problem wis gradle tomcat plugin. I updated the tomcat version to 8 and it worked.

    plugins {
        id "com.bmuschko.tomcat" version "2.2.2"
        id "java"
        id "eclipse"
        id "idea"
        id "war"
    }
    
    
    dependencies {
        providedCompile ('javax.servlet:javax.servlet-api:3.1.0')
        providedCompile ('javax.servlet.jsp:jsp-api:2.2')
        compile ('org.springframework:spring-webmvc:4.2.1.RELEASE')
    
        def tomcatVersion = '8.0.27'
        tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}",
                "org.apache.tomcat.embed:tomcat-embed-logging-juli:${tomcatVersion}",
                "org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}"
    
    }
    
    0 讨论(0)
  • 2020-12-03 11:26

    I alse get this problem, I use their method to handle it, but failed. my tomcat version i used are 7 and 8.0 , i got get this problem too. when i use tomcat 8.5, there is no problem. my project run successfully.

    0 讨论(0)
  • 2020-12-03 11:30

    I used providedCompile 'javax.servlet:javax.servlet-api:3.1.0' to solve the issue

    0 讨论(0)
  • 2020-12-03 11:33

    I am facing the same error when using Tomcat in Eclipse.

    I solve this by going to Servers tab > double click the Tomcat and uncheck the "Serve modules without publishing"

    0 讨论(0)
  • 2020-12-03 11:41

    A better servlet 3.0 api dependency entry in pom is available here - https://github.com/SpringSource/greenhouse/tree/servlet3, you should also mark the scope as provided, otherwise the jar will be included in your final war which will interfere with the jar provided by the container which is what seems to be happening in your case.

    0 讨论(0)
提交回复
热议问题