問題
之前在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