PhaseInterceptorChain和HttpServletRequest导致AbstractEndpointFactory ClassNotFoundException

时间:2013-10-18 13:47:41

标签: spring maven servlets jax-ws cxf

我需要能够reliably get client IP address in CXF JAX-WS,因此我将以下代码添加到我的网络服务中:

Message message = PhaseInterceptorChain.getCurrentMessage();
HttpServletRequest request = (HttpServletRequest)message.get(AbstractHTTPDestination.HTTP_REQUEST);
request.getRemoteAddr();

为了使构建成功完成,我必须添加以下导入:

import javax.servlet.http.HttpServletRequest;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.PhaseInterceptorChain;
import org.apache.cxf.transport.http.AbstractHTTPDestination;

并将以下依赖项添加到我的pom.xml

    <dependency> <!-- PhaseInterceptorChain & Message -->
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-api</artifactId>
        <version>2.4.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-transports-http</artifactId>
        <version>2.2.3</version>
    </dependency>
    <dependency> <!-- HttpServletRequest -->
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <!-- scope>provided</scope -->
    </dependency>

但是当我尝试部署生成的成功(!)版本时,Tomcat(7.0.42)会抛出异常并拒绝加载war文件:

org.springframework.beans.factory.BeanDefinitionStoreException: 
  Unexpected exception parsing XML document from class path resource [beans.xml]; 
    nested exception is org.springframework.beans.FatalBeanException: 
      Invalid NamespaceHandler class [org.apache.cxf.jaxws.spring.NamespaceHandler] for namespace [http://cxf.apache.org/jaxws]: 
        problem with handler class file or dependent class; 
          nested exception is java.lang.NoClassDefFoundError: org/apache/cxf/endpoint/AbstractEndpointFactory

...

Caused by: org.springframework.beans.FatalBeanException: 
  Invalid NamespaceHandler class [org.apache.cxf.jaxws.spring.NamespaceHandler] 
   for namespace [http://cxf.apache.org/jaxws]: 
    problem with handler class file or dependent class; 
     nested exception is java.lang.NoClassDefFoundError: org/apache/cxf/endpoint/AbstractEndpointFactory

...

Caused by: java.lang.ClassNotFoundException: org.apache.cxf.endpoint.AbstractEndpointFactory
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)

显然,我的pom.xml缺少一些在运行时提供必要模块的东西。我错过了什么?

1 个答案:

答案 0 :(得分:1)

神秘解决了。为了所有人的利益,我提供了问题的根本原因和解决方案:

问题在于这一行:

   <version>2.4.0</version>

也就是说,cxf-api工件的版本与整个项目中的CXF版本(以及pom.xml)不匹配。

解决问题所需要做的就是将该行更改为:

<version>${cxf.version}</version>

cxf.version之前定义为2.7.1

结论:CXF依赖项/ packages / plugins版本必须在整个pom.xml中匹配(否则你会得到一些“red herrings”)。