如何保留 SOAP Headers 以与 Mule 4 Web 服务消费者一起使用?

How To Preserve SOAP Headers to use with Mule 4 Web Service Consumer?

我已经使用现有的 WSDL 创建了一个体验 API,我在其中执行了几个步骤:

  1. 使用 Mule4 Web 服务消费者或
  2. 将 SOAP 请求消息as-is 传递到原始后端系统
  3. 调用另一个后端系统 (RESTful API) 并转换响应以匹配预期的 SOAP 响应消息

示例消息

<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns="urn:Acme/PublicService/V1" xmlns:ns0="urn:/Acme/BasicDataPublicService/V1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <env:Header>
        <a:Action s:mustUnderstand="1" xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope">urn:Acme/PublicService/V1/PublicService/SetCustomer</a:Action>
        <a:MessageID xmlns:a="http://www.w3.org/2005/08/addressing">urn:uuid:4afe0693-adea-4ede-bec9-10b694708d85</a:MessageID>
        <a:ReplyTo xmlns:a="http://www.w3.org/2005/08/addressing">
            <a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
        </a:ReplyTo>
        <VsDebuggerCausalityData xmlns="http://schemas.microsoft.com/vstudio/diagnostics/servicemodelsink">uIDPo8KxyyGpakdIj8o84JOeAMsAAAAAQBkt3vfAK0C4dDgn3rAKx/iXgqYosnhKv/OHgph9cXoACQAA</VsDebuggerCausalityData>
        <a:To s:mustUnderstand="1" xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope">http://316.820.517.311:36990/PublicInterface/Service</a:To>
        <AuthorizationToken xmlns="urn:Acme/Authorization/V1" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
            <CultureName>uk-UK</CultureName>
            <OnBehalfOf i:nil="true"></OnBehalfOf>
            <Password>****</Password>
            <UserName>sa-ACME</UserName>
        </AuthorizationToken>
    </env:Header>
    <env:Body>
        <ns:SetCustomer>Muli-Tier Message</ns:SetCustomer>
    </env:Body>
</env:Envelope>

根据Mulesoft KB;这需要在流程开始时执行额外的转换步骤。通过 SOAP Body 不是问题;然而,SOAP Header 是泡菜。特别是因为 KB 文档是 hard-coding 值;而在我的例子中,这些需要是动态的(即来自原始 SOAP 请求消息)。

我尝试按照描述将 Header 参数映射到一个变量,但我似乎做不到。

选项 1 将 Header 元素映射到变量的 Children 属性,导致变量存储 Null

%dw 2.0
output application/xml writeDeclaration=false, writeNilOnNull=true
ns ns0 http://www.w3.org/2005/08/addressing
ns s http://www.w3.org/2003/05/soap-envelope
---
headers: {
    ns0#Action @(s#mustUnderstands: payload.headers.Action.@mustUnderstands): payload.headers.Action as String default null,
    ns0#MessageID: payload.headers.MessageID as String default null,
    ns0#ReplyTo: {
        ns0#Address: payload.headers.ReplyTo.Address as String default null
    },
    VsDebuggerCausalityData: payload.headers.VsDebuggerCausalityData as String default null,
    ns0#To @(s#mustUnderstands: payload.headers.To.@mustUnderstands): payload.headers.To as String default null,
    AuthorizationToken: {
        CultureName: payload.headers.AuthorizationToken.CultureName as String default null,
        OnBehalfOf: payload.headers.AuthorizationToken.OnBehalfOf as String default null,
        Password: payload.headers.AuthorizationToken.Password as String default null,
        UserName: payload.headers.AuthorizationToken.UserName as String default null
    }
}

选项 2payload.headers 映射到变量,导致额外的标签;并丢失 XML 标签属性

%dw 2.0
output application/xml writeDeclaration=false, writeNilOnNull=true
ns ns0 http://www.w3.org/2005/08/addressing
ns s http://www.w3.org/2003/05/soap-envelope
---
headers: payload.headers

生成 SOAP 信封时 WebService 消费者连接器似乎有问题 Header。

尝试使用传递负载的 HTTP 请求连接器,而不是使用 Web 服务消费者连接器 as-is。

我会回来并在答案中添加更多详细信息,但这是解决该问题的方法:

%dw 2.0
output application/xml writeDeclaration=false, writeNilOnNull=true
---
headers: (payload.headers.headers mapObject (value, key) -> {
(value)
})