Add child elements to custom SOAP header in Spring-WS

后端 未结 4 831
清酒与你
清酒与你 2020-12-30 04:09

I am calling a SOAP webservice with Spring-WS. The webservice in question requires me to pass some information in the SOAP header as shown here:



        
相关标签:
4条回答
  • 2020-12-30 04:43

    I'm not pleased with this solution, but as it turns out you can actually cast the message to a SOAPMessage which gives you full access to all the SAAJ apis. From there you can build whatever elements you want inside the header.

    0 讨论(0)
  • 2020-12-30 04:45

    I came across the same issue, here's my solution but it will work only simple elements not for complex:

    template.marshalSendAndReceive(request, new WebServiceMessageCallback(){
        public void doWithMessage(WebServiceMessage message) throws IOException, TransformerException{
            SaajSoapMessage soapMessage = (SaajSoapMessage) message;
            SoapHeaderElement messageId =  soapMessage.getSoapHeader().addHeaderElement(new QName("http://www.w3.org/2005/08/addressing", "MessageID", "wsa"));
            messageId.setText("urn:abcdef1234");
        }
    });
    

    it produces following XML:

    <SOAP-ENV:Header>
      <wsa:MessageID>urn:abcdef1234</wsa:MessageID>
    </SOAP-ENV:Header>
    

    BTW javax.xml.soap.SOAPMessage can work too, see here: http://docs.oracle.com/javaee/5/tutorial/doc/bnbhr.html#bnbia

    0 讨论(0)
  • 2020-12-30 04:47

    I had the same issue and resolved it with the following snippet:

    Result result = ((SoapMessage) message).getSoapHeader().getResult();
    webServiceTemplate.getMarshaller().marshal(createCustomHeader(), result);
    

    The createCustomerHeader() method creates a JAXB bean which was generated from the XSD.

    0 讨论(0)
  • 2020-12-30 04:48
    final String datPrefix = "dat";
    final String datNamespaceUri = "UPRS/Services/IProviderDataManagement/Datatypes";
    
    final String mesPrefix = "mes";
    final String mesNamespaceUri = "UPRS/Services/IProviderDataManagement/Messages";
    
    SoapEnvelope se = s.getEnvelope();
    se.addNamespaceDeclaration(mesPrefix,
            mesNamespaceUri);
    se.addNamespaceDeclaration(datPrefix,
            datNamespaceUri);
    
    SoapMessage s = (SoapMessage) message;
    
    Element root = new Element("requestContext", mesPrefix, mesNamespaceUri);
    Element child = new Element("commandId", datPrefix, datNamespaceUri).addContent(guid);
    root.addContent(child);
    
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.transform(new JDOMSource(root), s.getSoapHeader().getResult());
    

    It produced output

    <SOAP-ENV:Header>
            <mes:requestContext xmlns:mes="UPRS/Services/IProviderDataManagement/Messages">
                <dat:commandId xmlns:dat="UPRS/Services/IProviderDataManagement/Datatypes">ba7b1e13-8a06-49b6-a264-fc0298f55f4f</dat:commandId>
            </mes:requestContext>
        </SOAP-ENV:Header>
    
    0 讨论(0)
提交回复
热议问题