1

我的要求就像

<soapenv:Header>
      <tns:SubHeader>
         <tns:version>xyz</tns:version>

      </tns:SubHeader>
</soapenv:Header>

我使用 Spring-WS 调用它。在这里我如何设置子标题(<tns:SubHeader>)值?

我的处理程序方法就像

SoapMessage soapMessage = (SoapMessage) messageContext.getRequest();
SoapHeader sh = soapMessage.getSoapHeader();
sh.addHeaderElement(new QName("http://test.com/v2", "SubHeader","tns"));

使用这个我可以添加<tns:SubHeader><soapenv:Header>我怎样才能添加<tns:version>xyz</tns:version>这个请求?

4

2 回答 2

1

使用拦截器,例如:

<int-ws:outbound-gateway
        uri="${services.mobileApprove}" request-channel="mobileApproveService"
        message-sender="mobileApproveMessageSender" 
        interceptor="soapHeaderElementAdder"/>

这是一个 bean 实现:(soapHeaderElementAdder是对spring bean的 id 引用)

public class
    UserContextSoapElementAdder implements org.springframework.ws.client.support.interceptor.ClientInterceptor {


Logger logger = LoggerFactory.getLogger(UserContextSoapElementAdder.class);

@Override
public boolean handleRequest(MessageContext messageContext) throws WebServiceClientException {
    try {
        Object si = messageContext.getProperty("si");

        SoapMessage soapMessage = (SoapMessage) messageContext.getRequest();

        SoapHeader sh = soapMessage.getSoapHeader();

        SoapHeaderElement soapHeaderInformation = sh.addHeaderElement(new QName("http://harmoni-integration.yamanyar.com.tr", "si",
                ""));


        //work with soapHeaderInformation 



    } catch (Exception e) {
        logger.error("Cannot add user context soap header!", e);
    }


    return true;
}

@Override
public boolean handleResponse(MessageContext messageContext) throws WebServiceClientException {
    return true;
}

@Override
public boolean handleFault(MessageContext messageContext) throws WebServiceClientException {
    return true;
}

}

于 2013-08-02T11:38:39.297 回答
1

您可以使用创建简单的 SOAP 标头mapped-request-headers

<ws:outbound-gateway uri="http://blah/blah"
    mapped-request-headers="foo" />

.

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Header foo="bar"/>
...

对于更复杂的事情,我相信您需要使用自定义消息工厂,或者可能是客户端拦截器来操作消息。

于 2013-06-09T18:07:01.677 回答