Spring Update SOAP Header with Two Elements

[亡魂溺海] 提交于 2019-11-30 10:00:28

问题


I am using Spring-WS and WebServiceTemplate to call SOAP WebService. Currently, i am facing issue while updating SOAP header.

Below is complete structure of SOAP request:

<SOAP:envlope>
    <SOAP:Header>
        <Security>
            <username>?</username>
            <password>?</password>
        </Security>
        <MessageData>
            <ClientIP>?</ClientIP>
        </MessageData>
    </SOAP:Header>
    <SOAP:Body>
        <Login/>
    </SOAP:Body>
</SOAP:envlope>

As shown above, i need to include 2 elements in Header. I have below code to update the Header of SOAP for WebServiceTemplate

Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(new StringSource(soapHeaderStr), ((SoapMessage) message).getSoapHeader().getResult());

But i got below error message when i tried to update header with two elements (String):

SOAPHeaderString

<Security>
   <username>?</username>
   <password>?</password>
</Security>
<MessageData>
   <ClientIP>?</ClientIP>
</MessageData>

Error

[Fatal Error] :1:197: The markup in the document following the root element must be well-formed.
ERROR:  'The markup in the document following the root element must be well-formed.'

The reason is that there is no root element for Security and MessageData elements and it shouldn't be.

Please help how can i update header in such case?

Thanks


回答1:


Finally, resolved it !!

I added dummy root while transformation and then removed that dummy root from SOAP Header.




回答2:


Had the same problem and managed to solve it, see here

Basically You can marshal the two elements directly into the soap header like this instead of tying to use a Transformer :

webServiceTemplate.getMarshaller().marshal(element1,soapHeader.getResult());
webServiceTemplate.getMarshaller().marshal(element2,soapHeader.getResult());

The marshaller mentioned here is a "org.springframework.oxm.jaxb.Jaxb2Marshaller" The element 1 and 2 above are the JAXB elements created using the generated Object factory class.



来源:https://stackoverflow.com/questions/15703646/spring-update-soap-header-with-two-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!