2013-10-04 39 views
1

我創建一個web服務與下列方式CXF:CXF驗證自定義處理

<cxf:cxfEndpoint id=XXXEndpoint" 
       serviceClass="com.Sth" 
       address="${webservices.url}/XXX" 
       wsdlURL="${wsdl.address}/services/XXX.wsdl" 
       endpointName="m:XXXPort" 
       serviceName="m:XXXService" 
       xmlns:m="http://com.sth/XXX"> 
    <cxf:properties> 
     <entry key="schema-validation-enabled" value="true" /> 
    </cxf:properties> 
</cxf:cxfEndpoint> 

它完美的作品,還增加了模式驗證。我無法添加自定義驗證處理程序。我怎樣才能做到這一點?

+0

要你的聲明:「我不能添加客戶validaton處理程序」,你嘗試過到現在呢? – Sikorski

+0

到目前爲止我曾嘗試這樣的: <條目鍵= 「JAXB驗證事件處理程序」> 像如下所述。但它不工作。 http://stackoverflow.com/questions/2195034/server-side-xml-validation-with-cxf-webservice 我乳寧它在Apache服務組合4.5.2 – markus

+0

當時org.example.MyCustomHandler不叫在所有?或者結果出乎意料? –

回答

4

我不知道你所說的自定義驗證處理程序是什麼意思。

如果要更改驗證錯誤處理,你可以創建類實現javax.xml.bind.ValidationEventHandler

比如我用這種辦法來防止JAXB從第一個遇到的錯誤引發的異常。我的自定義事件處理程序收集所有非致命的驗證錯誤,並在驗證整個傳入消息後拋出適當的異常。

Sample use of ValidationEventHandler

爲了使用您的自定義驗證事件處理程序,你應該添加JAXB驗證事件處理程序屬性:

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" 
    xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd 
     http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd"> 


    <jaxws:endpoint id="HTTPEndpoint" 
     implementor="org.dpytel.servicemix.cxf.wsdlfirst.PersonImpl" address="/PersonService" 
     wsdlLocation="wsdl/person.wsdl" endpointName="e:soap" serviceName="s:PersonService" 
     xmlns:e="http://servicemix.apache.org/samples/wsdl-first" xmlns:s="http://servicemix.apache.org/samples/wsdl-first"> 
     <jaxws:properties> 
      <entry key="schema-validation-enabled" value="true" /> 
      <entry key="jaxb-validation-event-handler"> 
       <bean class="org.dpytel.servicemix.cxf.wsdlfirst.MyCustomHandler"></bean> 
      </entry> 
     </jaxws:properties> 
    </jaxws:endpoint> 

</beans> 

駱駝CXF端點配置:

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cxf="http://camel.apache.org/schema/cxf" 
    xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://camel.apache.org/schema/cxf 
     http://camel.apache.org/schema/cxf/camel-cxf.xsd"> 

    <cxf:cxfEndpoint id="personEndpoint" address="/person" 
     serviceClass="org.apache.servicemix.samples.wsdl_first.Person" 
     wsdlURL="wsdl/person.wsdl"> 
     <cxf:properties> 
      <entry key="schema-validation-enabled" value="true" /> 
      <entry key="jaxb-validation-event-handler"> 
       <bean class="org.dpytel.servicemix.camel.MyCustomHandler" /> 
      </entry> 
     </cxf:properties> 
    </cxf:cxfEndpoint> 

</beans> 

例禁用驗證錯誤,只是處理記錄的驗證消息:

import java.util.logging.Logger; 
import javax.xml.bind.ValidationEvent; 
import javax.xml.bind.ValidationEventHandler; 

public class MyCustomHandler implements ValidationEventHandler { 

    private Logger logger = Logger.getLogger(this.getClass().getCanonicalName()); 

    public boolean handleEvent(ValidationEvent event) { 
     logger.severe("Error: " + event.getMessage()); 
     return true; 
    } 

} 

請注意,某些驗證錯誤會導致CXF跳過呼叫您的處理程序(請參閱DataReaderImpl.WSUIDValidationHandler.handleEvent(...)的詳細信息)。如果錯誤信息中包含你的處理程序將被跳過 「:ID」 的字符串,或者是下面的錯誤之一:

  • CVC-type.3.1.1
  • CVC-type.3.2.2
  • CVC -complex-type.3.1.1
  • CVC-複雜type.3.2.2

(坦白地說,好像在CXF一個骯髒的黑客,如果它是你的問題我將創造一個bug CXF團隊)。

如果你想要更多的錯誤處理定製你或許應該考慮編寫自己的Interceptor。可能執行這樣的驗證最佳的相位將是(PRE/USER/POST)_LOGICAL之一。

+0

嗨,不幸的是它根本不起作用。處理程序類是bever調用。任何想法? – markus

+0

我只注意到你使用Camel構造(cxf:cxfEndpoint) - 你從來沒有提到過。你真的需要駱駝端點嗎?如果不是,請嘗試將其替換爲我在答案中提供的純CXF端點定義。 –

+0

我必須是駱駝端點。我想這不會在這樣的配置中工作 – markus