[j2ee]解决在集成Apache CXF框架过程中遇到的问题cannot be cast to javax.servlet.Filter

问题

之前在j2ee(spring mvc+...)框架中集成Apache CXF成功(集成方法和修改的文件可参考 https://github.com/thinkgem/jeesite/pull/219 ),

但是这几天再次集成Apache CXF时却遇到了各种奇怪的报错,

比如之前各种正常servlet在启动tomcat报错:  cannot be cast to javax.servlet.Filter

或者是spring xml 配置文件中报 endpoint 什么的错等,百思不得其解,还以为eclipse环境坏了

原因分析

经过搜索发现是jar包与tomcat环境冲突,需要在引用CXF时排除冲突的jar包。

但是网上搜索到的排除jar似乎不完整,还是启动报错,因此通过git diff对比找到可能冲突的jar包。

解决方法

在pom.xml中引用Apache CXF时,重新设定引用方式为pom,并且排除各种冲突的文件。

冲突的文件可以通过git diff等方式对比项目目录下.classpath(Project Build Path Config),引入CXF后的.classpath相比引入前增加了以下可能冲突的jar包

<classpathentry kind="var" path="M2_REPO/javax/servlet/javax.servlet-api/3.1.0/javax.servlet-api-3.1.0.jar" sourcepath="M2_REPO/javax/servlet/javax.servlet-api/3.1.0/javax.servlet-api-3.1.0-sources.jar"/>

修改pom.xml的CXF引用为如下:

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>apache-cxf</artifactId>
            <version>3.1.4</version>
            <type>pom</type>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.geronimo.specs</groupId>
                    <artifactId>geronimo-servlet_3.0_spec</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>javax.servlet</groupId>
                    <artifactId>javax.servlet-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

参考资料

在maven项目中使用apache cxf中遇到异常 java.lang.ClassCastException: org.springframework.web.filter.CharacterEncodingFilter cannot be cast to javax.servlet.Filter-布布扣-bubuko.com

http://www.bubuko.com/infodetail-658239.html

tomcat - SEVERE: Exception springSecurityFilterChain... ClassCastException... DelegatingFilterProxy cannot be cast - Stack Overflow

http://stackoverflow.com/questions/7708288/severe-exception-springsecurityfilterchain-classcastexception-delegatingf

原文地址:https://www.cnblogs.com/snippet/p/solve-apache-cxf-integrate-error.html