简体   繁体   English

SpringServletContainerInitializer无法强制转换为javax.servlet.ServletContainerInitializer

[英]SpringServletContainerInitializer cannot be cast to javax.servlet.ServletContainerInitializer

I am attempting to move a xml-based Spring MVC app to a Java Configuration based app. 我试图将基于xml的Spring MVC应用程序移动到基于Java配置的应用程序。 There appears to be a mismatch with the various java.servlet classes available in maven. 似乎与maven中可用的各种java.servlet类不匹配。 For instance, some provide the addServlet() method and some do not. 例如,有些提供addServlet()方法,有些则不提供。

Here is my config class: 这是我的配置类:

public class MyWebAppInitializer implements WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext container) throws ServletException {

        AnnotationConfigWebApplicationContext rootContext =
                new AnnotationConfigWebApplicationContext();
        rootContext.register(JpaSandboxConf.class);

        ServletRegistration.Dynamic registration = container.addServlet("dispatcher", new DispatcherServlet());
        registration.setLoadOnStartup(1);
        registration.addMapping("/myapp/*");
    }
}

Which seems rather uncontroversial. 这似乎相当无可争议。 In order to get the 为了得到

container.addServlet()

method, I included this in my pom: 方法,我把它包含在我的pom中:

<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-servlet-api</artifactId>
    <version>7.0.30</version>
</dependency>

I tried this, but the ServletContext class from the entry below DID NOT WORK (in that it didnot provide the addServlet() method): 我试过这个,但是下面的条目中的ServletContext类没有工作(因为它没有提供addServlet()方法):

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>com.springsource.javax.servlet</artifactId>
    <version>2.5.0</version>
    <scope>provided</scope>
</dependency>

When I attempt to run this using 当我尝试使用它时

mvn clean tomcat7:run

I unhappily obtain 我不幸得到了

SEVERE: A child container failed during start
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Tomcat].StandardHost[localhost].StandardContext[/]]
    at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:252)
    at java.util.concurrent.FutureTask.get(FutureTask.java:111)
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1130)
    at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:782)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1568)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1558)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:722)
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Tomcat].StandardHost[localhost].StandardContext[/]]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
    ... 7 more
Caused by: java.lang.ClassCastException: org.springframework.web.SpringServletContainerInitializer cannot be cast to javax.servlet.ServletContainerInitializer
    at org.apache.catalina.startup.ContextConfig.getServletContainerInitializer(ContextConfig.java:1543)
    at org.apache.catalina.startup.ContextConfig.processServletContainerInitializers(ContextConfig.java:1464)
    at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1190)
    at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:825)
    at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:300)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
    at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5161)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    ... 7 more

I see that one class is coming from Spring framework and the other from javax.servlet, but once again, the method does not exist from the Spring provided class (which is frustrating, because this example exists within the Spring 3.2 documentation itself. 我看到一个类来自Spring框架而另一个来自javax.servlet,但是再一次,该方法不存在于Spring提供的类中(这令人沮丧,因为这个例子存在于Spring 3.2文档本身中。

I am using Spring 3.2. 我使用的是Spring 3.2。 I am NOT using Eclipse (all the examples are Eclipse based, I'm in IntelliJ). 我不使用Eclipse(所有示例都是基于Eclipse的,我在IntelliJ中)。 This is a maven project 这是一个maven项目

I DO appreciate your help. 我很感激你的帮助。

这里有一个更好的servlet 3.0 api依赖项输入 - https://github.com/SpringSource/greenhouse/tree/servlet3 ,你也应该标记所provided的范围,否则jar将包含在你的最后一战中干扰容器提供的罐子,这在您的情况下似乎正在发生。

I had a similar problem without Spring, where mvn tomcat7:run did not work, despite the servlet deploying to OpenShift just fine. 我有一个类似的问题没有Spring,其中mvn tomcat7:run无法正常工作,尽管servlet部署到OpenShift就好了。 As per Biju Kunjummen's answer, the problem for me was which servlet-api I was compiling against. 根据Biju Kunjummen的回答,对我来说问题是我正在编译的servlet-api。

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. 不是很清楚为什么,但它现在有效。

I was facing the same problem wis gradle tomcat plugin. 我面临同样的问题,因为gra gradle tomcat插件。 I updated the tomcat version to 8 and it worked. 我将tomcat版本更新为8并且它工作正常。

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

}

我使用providedCompile 'javax.servlet:javax.servlet-api:3.1.0'来解决这个问题

I am facing the same error when using Tomcat in Eclipse. 在Eclipse中使用Tomcat时,我遇到了同样的错误。

I solve this by going to Servers tab > double click the Tomcat and uncheck the "Serve modules without publishing" 我通过转到服务器选项卡>双击Tomcat并取消选中“不发布服务模块”来解决此问题

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. 我使用的tomcat版本是7和8.0,我也遇到了这个问题。 when i use tomcat 8.5, there is no problem. 当我使用tomcat 8.5时,没有问题。 my project run successfully. 我的项目成功运行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 org.springframework.web.SpringServletContainerInitializer无法转换为javax.servlet.ServletContainerInitializer — Eclipse Spring项目 - org.springframework.web.SpringServletContainerInitializer cannot be cast to javax.servlet.ServletContainerInitializer — Eclipse Spring Project Spring Tomcat7错误“org.springframework.web.SpringServletContainerInitializer无法强制转换为javax.servlet.ServletContainerInitializer” - Spring Tomcat7 error “org.springframework.web.SpringServletContainerInitializer cannot be cast to javax.servlet.ServletContainerInitializer” WebAppInitializer无法强制转换为javax.servlet.ServletContainerInitializer - WebAppInitializer cannot be cast to javax.servlet.ServletContainerInitializer wildfly 10 - javax.servlet.ServletContainerInitializer:提供者 org.springframework.web.SpringServletContainerInitializer 不是子类型 - wildfly 10 - javax.servlet.ServletContainerInitializer: Provider org.springframework.web.SpringServletContainerInitializer not a subtype OpenSessionInViewFilter无法强制转换为javax.servlet.Filter - OpenSessionInViewFilter cannot be cast to javax.servlet.Filter 查找javax.servlet-api的实现(ServletContainerInitializer和ServletContext) - Find Implementation of javax.servlet-api (ServletContainerInitializer and ServletContext) ClassCastException:org.springframework.web.servlet.DispatcherServlet无法转换为javax.servlet.Servlet - ClassCastException: org.springframework.web.servlet.DispatcherServlet cannot be cast to javax.servlet.Servlet 无法使用Spring WebFlow将对象强制转换为javax.servlet.http.HttpServletRequest - Object cannot be cast to javax.servlet.http.HttpServletRequest with Spring WebFlow org.springframework.web.filter.CharacterEncodingFilter无法转换为javax.servlet.Filter - org.springframework.web.filter.CharacterEncodingFilter cannot be cast to javax.servlet.Filter org.springframework.web.filter.HiddenHttpMethodFilter无法强制转换为javax.servlet.Filter - org.springframework.web.filter.HiddenHttpMethodFilter cannot be cast to javax.servlet.Filter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM