Apache CXF拦截器配置

arm*_*ino 2 java spring cxf jax-rs jax-ws

我创建了一个SOAP拦截器,如CXF文档中所述:

public class SoapMessageInterceptor extends AbstractSoapInterceptor {
    public SoapMessageInterceptor() {
        super(Phase.USER_PROTOCOL);
    }
    public void handleMessage(SoapMessage soapMessage) throws Fault {
        // ...
    }
}
Run Code Online (Sandbox Code Playgroud)

并在Spring的应用程序上下文中将其注册到总线:

  <cxf:bus>
    <cxf:inInterceptors>
      <ref bean="soapMessageInterceptor"/>
    </cxf:inInterceptors>
  </cxf:bus>

  <jaxws:endpoint id="customerWebServiceSoap"
        implementor="#customerWebServiceSoapEndpoint"
        address="/customerService"/>
Run Code Online (Sandbox Code Playgroud)

一切正常,直到我添加了REST服务:

  <jaxrs:server id="customerWebServiceRest" address="/rest">
    <jaxrs:serviceBeans>
      <ref bean="customerWebServiceRestEndpoint" />
    </jaxrs:serviceBeans>
  </jaxrs:server>
Run Code Online (Sandbox Code Playgroud)

问题是SOAP拦截器现在也在REST请求上被触发,这导致在调用REST服务时出现类强制转换异常.

<ns1:XMLFault xmlns:ns1="http://cxf.apache.org/bindings/xformat">
  <ns1:faultstring xmlns:ns1="http://cxf.apache.org/bindings/xformat">
    java.lang.ClassCastException: org.apache.cxf.message.XMLMessage
    cannot be cast to org.apache.cxf.binding.soap.SoapMessage
  </ns1:faultstring>
</ns1:XMLFault>
Run Code Online (Sandbox Code Playgroud)

有没有办法只通过配置将拦截器限制为SOAP消息?

更新

看起来我错过了描述这个的文档中的页面.向下滚动到JAXRS过滤器和CXF拦截器之间的差异

Ian*_*rts 10

您可以将拦截器连接到单个端点而不是总线:

<jaxws:endpoint id="customerWebServiceSoap"
    implementor="#customerWebServiceSoapEndpoint"
    address="/customerService">
  <jaxws:inInterceptors>
    <ref bean="soapMessageInterceptor"/>
  </jaxws:inInterceptors>
</jaxws:endpoint>
Run Code Online (Sandbox Code Playgroud)