使用拦截器,例如:
<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;
}
}