Set spring integration header into soap header in spring integration (without interceptor/threadlocal)

Set spring integration header into soap header in spring integration (without interceptor/ threadlocal)

是否可以在 spring 集成 xml 中从 spring 集成 header 设置 SOAP-ENV header?

我尝试了以下方法并期望 bId 填充到 soap headers

<int:chain id="soapcall" input-channel="soapchannel">      
        <int-xml:header-enricher>
            <int-xml:header name="bId" expression="headers['bId']"/>
        </int-xml:header-enricher>
          <int-ws:outbound-gateway  uri="${soap.url}" interceptor="myInterceptor">
          </int-ws:outbound-gateway> 
     </int:chain>

然而实际输出没有 header。见下文:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
                   xmlns:n1="http://namespace/n1">
   <SOAP-ENV:Header/>
   <SOAP-ENV:Body> ...
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

在调用出站网关之前,我还研究了链内的以下方法。似乎这个 header enricher 只像 header 那样采取肥皂作用。所以这会抛出异常 action headers not found

<int-ws:header-enricher >
            <int-ws:soap-action expression="headers['bId']"/>
          </int-ws:header-enricher>

我也调查了 this post。但是我无法在链中编译它。

我不确定 <int-xml:header-enricher> 是什么,但没关系。

您需要的是 <int-ws:outbound-gateway> 上的 header-mapper

它的代码如下:

@Override
protected void populateStandardHeaders(Map<String, Object> headers, SoapMessage target) {
    String soapAction = getHeaderIfAvailable(headers, WebServiceHeaders.SOAP_ACTION, String.class);
    if (!StringUtils.hasText(soapAction)) {
        soapAction = "\"\"";
    }
    target.setSoapAction(soapAction);
}

@Override
protected void populateUserDefinedHeader(String headerName, Object headerValue, SoapMessage target) {
    SoapHeader soapHeader = target.getSoapHeader();
    if (headerValue instanceof String) {
        QName qname = QNameUtils.parseQNameString(headerName);
        soapHeader.addAttribute(qname, (String) headerValue);
    }
}

因此,默认情况下它将 WebServiceHeaders.SOAP_ACTION 映射为标准映射,并且所有那些用户定义的 String 并且仅作为目标 SOAP-ENV:Headerattribute元素.

那些用户定义的 headers 可以映射为:

<xsd:attribute name="mapped-request-headers" type="xsd:string">
            <xsd:annotation>
                <xsd:documentation><![CDATA[
Comma-separated list of names of SOAP Headers to be mapped from the SOAP request into the MessageHeaders.
This can only be provided if the 'header-mapper' reference is not being set directly. The values in
this list can also be simple patterns to be matched against the header names (e.g. "foo*" or "*foo").
                        ]]></xsd:documentation>
            </xsd:annotation>
        </xsd:attribute>

如果您需要将 sub-elements 填充到 <SOAP-ENV:Header>,您别无选择,除非扩展 DefaultSoapHeaderMapper 并覆盖 populateUserDefinedHeader() 并使用 SoapHeader.addHeaderElement() API.

另请查看 Reference Manual