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

5
我正在使用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中应该在哪里使用这段代码吗?谢谢。


有像MessageBodyReaders和Writers这样的接口。我相信CXF一定有一些可插拔的选项,可以指定您的messagebodywriter。这个消息体编写器可以有您自己的自定义代码。 - Sikorski
1个回答

5
您可以在服务器上使用入站拦截器或在客户端上使用出站拦截器,在取消编组之前清理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                     |                                                           |
---------------------------------------------------------------------------------------

请记住,在解组(传入)之前或编组(传出)之后必须执行此操作。 这里您可以找到所有详细信息,以及使用inteceptors所需的示例。 更新 经过更多研究,我找到了以下内容:
  1. 使用Spring的解决方案
  2. 不使用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>
       ...

没有问题,你可以给我点踩,但请至少解释一下原因。 - Alkis Kalogeris

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接