Announcement Announcement Module
Collapse
No announcement yet.
Spring Update SOAP Header with Two Elements Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring Update SOAP Header with Two Elements

    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:

    Code:
        <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

    Code:
        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

    Code:
        <Security>
           <username>?</username>
           <password>?</password>
        </Security>
        <MessageData>
           <ClientIP>?</ClientIP>
        </MessageData>
    Error

    Code:
        [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

  • #2
    I've used the Wss4jSecurityInterceptor to set the UsernameToken. Add it the WebServiceTemplate interceptors.
    This described in section 7.3.3.2. Adding Username Token of the user reference.
    Code:
     <bean id="securityInterceptor"
            class="org.springframework.ws.soap.security.wss4j.Wss4jSecurityInterceptor"
            p:securementActions="UsernameToken"
            p:securementUsername="#{usernameToken['username']}"
            p:securementPassword="#{usernameToken['password']}"
            p:securementPasswordType="#{T(org.apache.ws.security.WSConstants).PW_TEXT}"/>
    If think you could also set the <MessageData> with an interceptor.

    Comment

    Working...
    X