如何保留SOAP标头以与Mule 4 Web Service使用者一起使用?

问题描述

我使用现有的WSDL创建了一个Experience API,在此之前我要执行几个步骤:

  1. 使用Mule4 Web服务使用者将SOAP请求消息原样传递到原始后端系统,或者
  2. 调用一个后端系统(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>

SOAP Header Elements as they appear in Debug Mode

根据Mulesoft KB;这需要在流程开始时执行其他转换步骤。通过SOAP主体不是问题。但是,SOAP头是一个泡菜。特别是因为KB文档对值进行了硬编码;而在我看来,这些功能必须是动态的(即来自原始SOAP请求消息)。

我尝试按照所述方法将标头参数映射到变量,但似乎无法理解。

选项1 将页眉元素映射到变量的子级属性,将导致变量存储为空

%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
    }
}

Variable (vars.headerParameters) used to store SOAP Header showing Null Values,while Variable (vars.queryParameters.dateFrom) storing a Value from SOAP Body has a Value

选项2 payload.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

Variable (vars.headerParameters) used to store SOAP Header showing double tags without tag attributes,while Variable (vars.queryParameters.dateFrom) storing a Value from SOAP Body has a Value

解决方法

在生成SOAP信封标头时,WebService Consumer连接器似乎存在问题。

请尝试使用HTTP请求连接器按原样传递有效负载,而不是使用WebService Consumer连接器。

,

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

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