非法字符实体:扩展字符(代码0xe

时间:2013-10-30 10:51:04

标签: java cxf special-characters

我使用CXF连接SOAP Web服务。有时在请求服务器时我得到非法字符实体:扩展字符代码0xe 异常。

我发现由于Xml中的非法字符,会发生此异常。我也找到了解决方案

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

但我不知道如何在CXF中应用它。有人可以告诉我在CXF中我必须使用这个代码。谢谢你的建议。

1 个答案:

答案 0 :(得分: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您可以找到所有细节,以及使用感知器的必要示例。

<强>更新

经过一些研究,我发现了这些:

  1. Solution with spring
  2. Solution without spring
  3. 从第二个链接复制并粘贴(第一个是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>
           ...