2012-10-12 111 views
2

我創建爲CXF docs描述的SOAP攔截器:的Apache CXF攔截器配置

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

,並在Spring的應用程序上下文中有公交註冊吧:

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

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

所有工作正常,直到我添加了一個REST服務:

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

問題在於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> 

有什麼方法可以通過配置將攔截器限制爲SOAP消息嗎?

更新

看起來像我錯過了頁面描述這個文檔。 向下滾動到Difference between JAXRS filters and CXF interceptors

回答

10

您可以將攔截到一個單獨的端點,而不是公交車:

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

但是如果我想在端點之間共享攔截器配置?將它們添加到cxf:bus或jaxws:server不會產生與將它們添加到端點的行爲相同的行爲。 – Dormouse

4

你可以嘗試配置您的攔截器是這樣的:

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

通過定義總線,它根據文檔的name,標識總線作爲一種獨特的Spring豆。然後在你的JAX-WS端點配置需要指定總線引用該名稱:

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

bus應在此JAX-WS終點纔有效。