如何将对象添加到org.
springframework.ws.WebServiceMessage的soap头中
这是我想要的结构:
<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.