简体   繁体   English

Apache CXF拦截器配置

[英]Apache CXF interceptor configuration

I created a SOAP interceptor as described in CXF docs : 我创建了一个SOAP拦截器,如CXF文档中所述

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

and registered it with the bus in Spring's application context: 并在Spring的应用程序上下文中将其注册到总线:

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

  <jaxws:endpoint id="customerWebServiceSoap"
        implementor="#customerWebServiceSoapEndpoint"
        address="/customerService"/>

All was working fine until I added a REST service: 一切正常,直到我添加了REST服务:

  <jaxrs:server id="customerWebServiceRest" address="/rest">
    <jaxrs:serviceBeans>
      <ref bean="customerWebServiceRestEndpoint" />
    </jaxrs:serviceBeans>
  </jaxrs:server>

The problems is that the SOAP interceptor is now being triggered on REST requests also, which results in a class cast exception when the REST service is invoked. 问题是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>

Is there any way to restrict the interceptor to SOAP messages only through configuration? 有没有办法只通过配置将拦截器限制为SOAP消息?

Update 更新

Looks like I missed the page in the docs that describes this. 看起来我错过了描述这个的文档中的页面。 Scroll down to Difference between JAXRS filters and CXF interceptors 向下滚动到JAXRS过滤器和CXF拦截器之间的差异

You can attach interceptors to an individual endpoint rather than to the bus: 您可以将拦截器连接到单个端点而不是总线:

<jaxws:endpoint id="customerWebServiceSoap"
    implementor="#customerWebServiceSoapEndpoint"
    address="/customerService">
  <jaxws:inInterceptors>
    <ref bean="soapMessageInterceptor"/>
  </jaxws:inInterceptors>
</jaxws:endpoint>

You can try to configure your interceptor like this: 您可以尝试像这样配置拦截器:

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

By defining the name of the bus, which according to documentation, identifies a bus as a unique Spring bean. 通过定义总线的name ,根据文档,将总线标识为唯一的Spring bean。 Then in your JAX-WS endpoint configuration you need to specify the bus referencing to that name: 然后在JAX-WS端点配置中,您需要指定引用该名称的总线:

  <jaxws:endpoint id="customerWebServiceSoap"
        implementor="#customerWebServiceSoapEndpoint"
        address="/customerService"
        bus="someBus"/>

And this bus should work only on this JAX-WS endpoint. bus应仅适用于此JAX-WS端点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM