2013-10-30 51 views
4

我使用CXF連接SOAP的Web服務有些時候,同時要求我得到非法字符實體服務器:擴展字符代碼0xe例外非法字符實體:擴展字符(代碼0xe

我發現。 ,由於在XML中非法的字符,該異常發生。此外,我找到了解決這個

XMLOutputFactory f = new WstxOutputFactory(); 
    f.setProperty(WstxOutputProperties.P_OUTPUT_INVALID_CHAR_HANDLER, 
    new InvalidCharHandler.ReplacingHandler(' ')); 
    XMLStreamWriter sw = f.createXMLStreamWriter(...); 

但我不知道如何在CXF應用它。有人能告訴我在哪裏,我必須使用CXF此代碼。謝謝建議。

+0

有喜歡MessageBodyReaders和作家接口。我確信CXF必須有一些可以插入的選項,通過它你可以指定你的messagebodywriter。此郵件正文編寫者可以使用您的自定義代碼 – Sikorski

回答

2

您可以在服務器上使用傳入攔截器或在客戶端上使用傳出攔截器,並在解組發生之前清理xml。

傳入階段

------------------------------------------------------------------------------ 
|Phase     | Functions          | 
------------------------------------------------------------------------------ 
|RECEIVE     | Transport level processing      | 
|(PRE/USER/POST)_STREAM | Stream level processing/transformations   | 
|READ |     | This is where header reading typically occurs | 
|(PRE/USER/POST)_PROTOCOL | Protocol processing, such as JAX-WS SOAP handlers| 
|UNMARSHAL    | Unmarshalling of the request      | 
|(PRE/USER/POST)_LOGICAL | Processing of the umarshalled request   | 
|PRE_INVOKE    | Pre invocation actions       | 
|INVOKE     | Invocation of the service      | 
|POST_INVOKE    | Invocation of the outgoing chain if there is one | 
------------------------------------------------------------------------------ 

引出相位

--------------------------------------------------------------------------------------- 
|Phase     | Functions             | 
--------------------------------------------------------------------------------------- 
|SETUP     | Any set up for the following phases      | 
|(PRE/USER/POST)_LOGICAL | Processing of objects about to marshalled     | 
|PREPARE_SEND    | Opening of the connection         | 
|PRE_STREAM    |               | 
|PRE_PROTOCOL    | Misc protocol actions          | 
|WRITE     | Writing of the protocol message, such as the SOAP Envelope| 
|MARSHAL     | Marshalling of the objects        | 
|(USER/POST)_PROTOCOL  | Processing of the protocol message      | 
|(USER/POST)_STREAM  | Processing of the byte level message      | 
|SEND      |               | 
--------------------------------------------------------------------------------------- 

請記住,你將不得不解編(進入)之前或編組(離開)之後做到這一點。

Here你可以找到所有的細節,以及使用inteceptors的必要示例。

更新

一些調查研究後,我發現這些:

  1. Solution with spring
  2. Solution without spring

複製,並從第二個鏈接粘貼(第一個是SO回答)

package tmp; 

public class MyWstxOutputFatory extends WstxOutputFactory { 
    public MyWstxOutputFatory() { 
     setProperty(com.ctc.wstx.api.WstxOutputProperties.P_OUTPUT_INVALID_CHAR_HANDLER, 
        new 
com.ctc.wstx.api.InvalidCharHandler.ReplacingHandler(' ')); 
    } 
} 

,並用它在你的JAXWS配置

<bean id="outStaxFactory" class="tmp.MyWstxOutputFatory"/> 

    <!-- jaxws:client or server --> 
     ... 
     <jaxws:properties> 
      <entry key="javax.xml.stream.XMLOutputFactory"> 
       <ref bean="outStaxFactory"/> 
      </entry> 
     </jaxws:properties> 
     ... 
+0

downvote沒有問題,但至少給出解釋。 – alkis