17.4.6. 仅在 POJO 模式中可用


转发Headers=true 表示标头转发意图。对给定标头进行转发的实际决定是否被委派给了实施 MessageHeadersRelay 接口的可插拔实例。将咨询一个对 MessageHeadersRelay 的整合实施,以确定是否需要转发标题。已有一个 SoapMessageHeadersRelay 的实现,它将自身绑定到知名的 SOAP 命名空间。目前,只过滤掉带外的标头,且在转发 Headers=true 时,始终会转发 带外的标头。如果线上有一个标头,则该命名空间对于运行时未知,则会使用一个回退 DefaultMessageHeadersRelay,只允许转发所有标头。

转发Headers=false 设置指定应丢弃所有带外和带外的标头。

您可以插入您自己的 MessageHeadersRelay 实现覆盖,或向中继列表中添加额外的设置。要覆盖预加载的中继实例,只需确保您的 MessageHeadersRelay 实施服务与您要覆盖的命名空间相同。另请注意,覆盖中继必须与您要覆盖的所有命名空间作为您要覆盖的命名空间提供服务,否则,在路由启动时出现运行时异常会抛出,因为这会在命名空间引入了一个模糊性来转发实例映射。

<cxf:cxfEndpoint ...>
   <cxf:properties>
     <entry key="org.apache.camel.cxf.message.headers.relays">
       <list>
         <ref bean="customHeadersRelay"/>
       </list>
     </entry>
   </cxf:properties>
 </cxf:cxfEndpoint>
 <bean id="customHeadersRelay" class="org.apache.camel.component.cxf.soap.headers.CustomHeadersRelay"/>
Copy to Clipboard Toggle word wrap

查看演示如何在这里转发/drop 标头的测试:

https://github.com/apache/camel/blob/main/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/soap/headers/CxfMessageHeadersRelayTest.java

  • 支持 POJOPAYLOAD 模式。在 POJO 模式中,只有带外消息标头才会可用于过滤,因为 CXF 已将标头处理并从标头列表中删除。in-band 标头被合并到 POJO 模式的 MessageContentList 中。camel-cxf 组件发出任何试图从 MessageContentList 中删除 in-band 标头的任何尝试。如果需要过滤带外标头,请使用 PAYLOAD 模式或插入到(pretty straightforward) CXF 拦截器/JAXWS Handler 到 CXF 端点。
  • Message Header Relay 机制已合并到 CxfHeaderFilterStrategy 中。转发Headers 选项、其语义和默认值保持不变,但它是 CxfHeaderFilterStrategy 属性。以下是配置它的示例:
@Bean
public HeaderFilterStrategy dropAllMessageHeadersStrategy() {
    CxfHeaderFilterStrategy headerFilterStrategy = new CxfHeaderFilterStrategy();
    headerFilterStrategy.setRelayHeaders(false);
    return headerFilterStrategy;
}
Copy to Clipboard Toggle word wrap

然后,您的端点可以引用 CxfHeaderFilterStrategy

@Bean
public CxfEndpoint routerNoRelayEndpoint(HeaderFilterStrategy dropAllMessageHeadersStrategy) {
    CxfSpringEndpoint cxfEndpoint = new CxfSpringEndpoint();
    cxfEndpoint.setServiceClass(org.apache.camel.component.cxf.soap.headers.HeaderTester.class);
    cxfEndpoint.setAddress("/CxfMessageHeadersRelayTest/HeaderService/routerNoRelayEndpoint");
    cxfEndpoint.setWsdlURL("soap_header.wsdl");
    cxfEndpoint.setEndpointNameAsQName(
        QName.valueOf("{http://apache.org/camel/component/cxf/soap/headers}SoapPortNoRelay"));
    cxfEndpoint.setServiceNameAsQName(SERVICENAME);
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put("dataFormat", "PAYLOAD");
    cxfEndpoint.setProperties(properties);
    cxfEndpoint.setHeaderFilterStrategy(dropAllMessageHeadersStrategy);
    return cxfEndpoint;
}

@Bean
public CxfEndpoint serviceNoRelayEndpoint(HeaderFilterStrategy dropAllMessageHeadersStrategy) {
    CxfSpringEndpoint cxfEndpoint = new CxfSpringEndpoint();
    cxfEndpoint.setServiceClass(org.apache.camel.component.cxf.soap.headers.HeaderTester.class);
    cxfEndpoint.setAddress("http://localhost:" + port + "/services/CxfMessageHeadersRelayTest/HeaderService/routerNoRelayEndpointBackend");
    cxfEndpoint.setWsdlURL("soap_header.wsdl");
    cxfEndpoint.setEndpointNameAsQName(
        QName.valueOf("{http://apache.org/camel/component/cxf/soap/headers}SoapPortNoRelay"));
    cxfEndpoint.setServiceNameAsQName(SERVICENAME);
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put("dataFormat", "PAYLOAD");
    cxfEndpoint.setProperties(properties);
    cxfEndpoint.setHeaderFilterStrategy(dropAllMessageHeadersStrategy);
    return cxfEndpoint;
}
Copy to Clipboard Toggle word wrap

然后,按照以下方式配置路由:

rom("cxf:bean:routerNoRelayEndpoint")
    .to("cxf:bean:serviceNoRelayEndpoint");
Copy to Clipboard Toggle word wrap
  • MessageHeadersRelay 接口已经稍有变化,并且已重命名为 MessageHeaderFilter。它是 CxfHeaderFilterStrategy 的一个属性。以下是配置用户定义的消息标头过滤器的示例:
@Bean
public HeaderFilterStrategy customMessageFilterStrategy() {
    CxfHeaderFilterStrategy headerFilterStrategy = new CxfHeaderFilterStrategy();
    List<MessageHeaderFilter> headerFilterList = new ArrayList<MessageHeaderFilter>();
    headerFilterList.add(new SoapMessageHeaderFilter());
    headerFilterList.add(new CustomHeaderFilter());
    headerFilterStrategy.setMessageHeaderFilters(headerFilterList);
    return headerFilterStrategy;
}
Copy to Clipboard Toggle word wrap
  • 除了 转发标头 外,还可在 CxfHeaderFilterStrategy 中配置以下属性。
Expand
名称必需描述

relayHeaders

所有邮件标题将由 Message Header Filters Type 进行处理:布尔值 默认值:true

relayAllMessageHeaders

所有邮件标题将传播(不需要由消息标头过滤器处理) 类型布尔值 默认值:false

allowFilterNamespaceClash

如果两个过滤器在激活命名空间中重叠,则属性控制应如何处理它。如果值为 true,则最后一个胜过。如果值为 false,它将抛出异常 类型布尔值 默认值:false

返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

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

让开源更具包容性

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

關於紅帽

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

Theme

© 2025 Red Hat