搜索

18.7. 如何让 camel-cxf 响应以 xml 处理指令开头

download PDF

如果您使用一些 SOAP 客户端,如 PHP,则会出现这类错误,因为 CXF 没有添加 XML 处理指令 < ?xml version="1.0" encoding="utf-8"?>:

Error:sendSms: SoapFault exception: [Client] looks like we got no XML document in [...]

要解决这个问题,您只需要告诉 StaxOutInterceptor 为您编写 XML 启动文档,如以下 WriteXmlDeclarationInterceptor 所述:

public class WriteXmlDeclarationInterceptor extends AbstractPhaseInterceptor<SoapMessage> {
    public WriteXmlDeclarationInterceptor() {
        super(Phase.PRE_STREAM);
        addBefore(StaxOutInterceptor.class.getName());
    }

    public void handleMessage(SoapMessage message) throws Fault {
        message.put("org.apache.cxf.stax.force-start-document", Boolean.TRUE);
    }

}

另外,您还可以为它添加一个消息标头,如 CxfConsumerTest 所示:

 // set up the response context which force start document
 Map<String, Object> map = new HashMap<String, Object>();
 map.put("org.apache.cxf.stax.force-start-document", Boolean.TRUE);
 exchange.getOut().setHeader(Client.RESPONSE_CONTEXT, map);
Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.