如何将对象添加到org.springframework.ws.WebServiceMessage
的soap header 中
这是我希望最终得到的结构:
<soap:Header>
<credentials xmlns="http://example.com/auth">
<username>username</username>
<password>password</password>
</credentials>
</soap:Header>
最佳答案
基本上,您需要在客户端中使用 WebServiceMessageCallback
在消息创建之后但发送之前对其进行修改。 @skaffman已相当准确地描述了其余代码,因此整个内容可能看起来像这样:
public void marshalWithSoapActionHeader(MyObject o) {
webServiceTemplate.marshalSendAndReceive(o, new WebServiceMessageCallback() {
public void doWithMessage(WebServiceMessage message) {
try {
SoapMessage soapMessage = (SoapMessage)message;
SoapHeader header = soapMessage.getSoapHeader();
StringSource headerSource = new StringSource("<credentials xmlns=\"http://example.com/auth\">\n +
<username>"+username+"</username>\n +
<password>"+password"+</password>\n +
</credentials>");
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(headerSource, header.getResult());
} catch (Exception e) {
// exception handling
}
}
});
}
就我个人而言,我发现Spring-WS很难满足这样的基本需求,他们应该修复SWS-479。