我的要求就像
<soapenv:Header>
<tns:SubHeader>
<tns:version>xyz</tns:version>
</tns:SubHeader>
</soapenv:Header>
我使用Spring-WS调用它。
在这里我如何设置subheader (<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>
?
答案 0 :(得分:1)
您可以使用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"/>
...
对于任何更复杂的东西,我相信您需要使用自定义消息工厂,或者可能需要客户端拦截器来操作消息。
答案 1 :(得分: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;
}
}