【问题标题】:Header Values are coming as null in Apache camel ExchangeApache camel Exchange 中的标头值为空
【发布时间】:2016-09-24 18:42:38
【问题描述】:

以下是我的网络服务请求、Route 和 Request-Validator,

网络服务请求:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <stlh:SabreHeader xmlns:stlh="http://services.sabre.com/STL_Header/v02_01">
      <stlh:Service version="1.0.0">GetHotelMediaRQ</stlh:Service>
      <stlh:Identification>
        <stlh:CustomerID>CID12345</stlh:CustomerID>
        <stlh:CustomerAppID>AppTest</stlh:CustomerAppID>
        <stlh:ConversationID>05EFPElI2A4KudU75863JIxqAhQJtAx0</stlh:ConversationID>
        <stlh:MessageID>4DTTQaHGSifFUtmSoMHAiq</stlh:MessageID>
        <stlh:TimeStamp>2014-11-07T14:45:42.725-06:00</stlh:TimeStamp>
      </stlh:Identification>
    </stlh:SabreHeader>
    <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd oasis-200401-wss-wssecurity-secext-1.0.xsd">
      <wsse:BinarySecurityToken EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary" wsu:Id="athId">${athId}</wsse:BinarySecurityToken>
    </wsse:Security>
  </soap:Header>
  <soap:Body>
    <GetHotelMediaRQ xmlns="http://services.sabre.com/hotel/media/v1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0.0" xsi:schemaLocation="http://services.sabre.com/hotel/media/v1 GetHotelMediaRQ.xsd">
      <HotelRefs>
        <HotelRef HotelCode="184769" CodeContext="Sabre">
          <ImageRef MaxImages="1">
            <Images>
              <Image Type="ORI"/>
            </Images>
           <AdditionalInfo>
              <Info Type="CAPTION">true</Info>
            </AdditionalInfo>
            <Languages>
              <Language Code="EN"/>
            </Languages>
          </ImageRef>
        </HotelRef>
      </HotelRefs>
    </GetHotelMediaRQ>
  </soap:Body>
</soap:Envelope>

请求验证器:

  public void validate(GetHotelMediaRQ request, Exchange exchange) throws Exception {
        TransactionContext context = BusExtensions.getTransactionContext(exchange);
        Collection<HotelRef> hotelRefList = getInstance().convert(request, Collection.class);
        Set<Property> properties = new HashSet<>();
        String customerAppId = exchange.getIn().getHeader("customerAppID", String.class);
        String customerId = exchange.getIn().getHeader("customerID", String.class);

但是当我尝试通过 Exchange 对象访问时,customerAppId(AppTest) 和 CustomerId(CI12345) 将变为 null。

【问题讨论】:

  • 尝试使用“CustomerID”或“stlh:CustomerID”而不是“customerID”
  • 我尝试了两个选项 pat ,它不起作用,当我调试时,值不存在于 Header 中!
  • 那么最好将它设置在body中并尝试通过

标签: java apache-camel apache-camel-cdi


【解决方案1】:

“自定义”Soap headers 不会复制到 camel header 中。您必须手动将肥皂头添加到骆驼交换头中。

方法一)

CamelCxfMessage -您可以提取/处理骆驼交换标头中存在的自定义soap标头camel cxf消息

骆驼 - SoapMessage soapMessage = (SoapMessage)exchange.getIn().getHeader("CamelCxfMessage");

这将为您提供soap消息及其soapMessage.getExchange,并尝试从soap消息中获取soap标头并进行处理。

方法2)

Camel Cxf 绑定 - 您可以在端点定义中使用骆驼 cxf 绑定功能,例如 cxfBinding=#bindingName

创建一个类并使用 org.apache.camel.component.cxf.DefaultCxfBinding 进行扩展,bean 名称应为 bindingName

它有一个你必须覆盖的方法 - propagateHeadersFromCxfToCamel(camelmessage ,cxfmessage ,exchage )。

在这里获取您的soap标头并将其放入带有标识符的骆驼标头中,并在处理器或具有相同标识符的路由中访问骆驼交换标头中的标头。

【讨论】:

    【解决方案2】:

    将 org.apache.camel 的日志记录设置为 DEBUG,并将记录标头值,您可以确定组件是否正在删除它们。

    此外,您可能正在使用 cxf soap 端点。在此处查看文档的 [Description of relayHeaders 选项] 部分:

    http://camel.apache.org/cxf.html

    【讨论】:

      【解决方案3】:

      我必须提取标题信息,但在第一次尝试时在对象中得到了 null。然后过了一会儿我可以把它捞出来。这里是如何(在处理器中):

      @Override
      public void process(Exchange exc) throws Exception {
      
          @SuppressWarnings("unchecked")
          List<SoapHeader> headers = exc.getIn().getHeader(Header.HEADER_LIST, List.class);
          for (int i=0; i < headers.size(); i++) {
      
              if (headers.get(i).getObject() instanceof ElementNSImpl) {
                  ElementNSImpl elementNSImpl = (ElementNSImpl) headers.get(i).getObject();
                  Node firstChild = elementNSImpl.getFirstChild();
                  log.trace("header: name=" + elementNSImpl.getLocalName() + ", value=" + firstChild.getNodeValue());
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-05-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-06
        相关资源
        最近更新 更多