java – 将SoapHeader添加到org.springframework.ws.WebServiceMessage

前端之家收集整理的这篇文章主要介绍了java – 将SoapHeader添加到org.springframework.ws.WebServiceMessage前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何将对象添加到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.

猜你在找的Java相关文章