org.springframework.web.filter.HiddenHttpMethodFilter 不能转换为 javax.servlet.Filter

Posted

技术标签:

【中文标题】org.springframework.web.filter.HiddenHttpMethodFilter 不能转换为 javax.servlet.Filter【英文标题】:org.springframework.web.filter.HiddenHttpMethodFilter cannot be cast to javax.servlet.Filter 【发布时间】:2012-11-20 03:21:08 【问题描述】:

我有一个基于 spring 框架 3 的 maven webapp,当我使用 maven-jetty-plugin 在码头上运行应用程序时,通过运行命令 mvn -e clean jetty:run 它运行正常,但是当我使用 tomcat-maven-plugin 在 tomcat 上运行它时,通过运行命令mvn -e clean tomcat:run 它给了我以下异常:

java.lang.ClassCastException: org.springframework.web.filter.HiddenHttpMethodFilter cannot be cast to javax.servlet.Filter
        at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:275)
        at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:422)
        at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:115)
        at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4001)
        at org.apache.catalina.core.StandardContext.start(StandardContext.java:4651)
        at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
        at org.apache.catalina.core.StandardHost.start(StandardHost.java:785)
        at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
        at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:445)
        at org.apache.catalina.startup.Embedded.start(Embedded.java:825)
        at org.codehaus.mojo.tomcat.AbstractRunMojo.startContainer(AbstractRunMojo.java:558)
        at org.codehaus.mojo.tomcat.AbstractRunMojo.execute(AbstractRunMojo.java:255)
        at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
        at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
        at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:319)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
        at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
        at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
        at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
        at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)

以下是我在web.xml 中配置过滤器的方式:

<!-- Filters -->
<filter>
    <filter-name>sitemesh</filter-name>
    <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
</filter>
<filter>
    <filter-name>methodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>sitemesh</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
</filter-mapping>
<filter-mapping>
    <filter-name>methodFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

有人可以帮助我了解为什么会出现此错误并帮助我解决它吗?

谢谢。

【问题讨论】:

见***.com/questions/7041761/… @axtavt 我刚刚检查过,&lt;scope&gt;provided&lt;/scope&gt; 在应用程序的pom.xml 中被注释为&lt;dependency&gt; &lt;groupId&gt;javax.servlet&lt;/groupId&gt; &lt;artifactId&gt;com.springsource.javax.servlet&lt;/artifactId&gt; &lt;version&gt;2.5.0&lt;/version&gt; &lt;!-- &lt;scope&gt;provided&lt;/scope&gt; --&gt; &lt;/dependency&gt;。当我取消注释&lt;scope&gt;provided&lt;/scope&gt; 时,java 源代码开始出现javax.servlet.http.HttpServletRequest 的编译错误,而来自javax.servlet.http 的其他类无法再导入。我从来没有遇到过将scope 设置为provided 的编译问题。 @axtavt 但是,如果 scope 被设置为 provided 以外的任何东西,com.springsource.javax.servlet 是问题所在,码头也应该不会抱怨吗? 【参考方案1】:
    先见First confluence servlet plugin, ClassCastException problem
<dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>servlet-api</artifactId>
   <version>2.5</version>
   <scope>provided</scope>
</dependency>

注意

<scope>provided</scope>

然后你需要检查依赖树,排除其他 servlet-api

【讨论】:

以上是关于org.springframework.web.filter.HiddenHttpMethodFilter 不能转换为 javax.servlet.Filter的主要内容,如果未能解决你的问题,请参考以下文章

解决post请求乱码问题

乱码及restful

字符编码过滤器

spring mvc 坑之PUT方法接收不到请求参数

Spring MVC rest接收json中文格式数据显示乱码