SpringServletContainerInitializer cannot be cast to javax.servlet.ServletContainerInitializer

寵の児 提交于 2019-11-27 14:21:51

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.

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.

Saurabh

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

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}"

}

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"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!