Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring WebServiceTemplate's marshalSendAndReceive method sends SOAP header as null when sending Request

I am developing a SOAP Client in java.My SOAPRequest is given below :

SOAP Request

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:com="http://myWs.com/com" xmlns:re="http://myWs.com/myAlert">
   <soapenv:Header>
      <com:MYHeader>
         <com:Auth>
            <com:MyID>1312</com:MyID>
            <com:Pwd>test213</com:Pwd>
         </com:Auth>
      </com:MYHeader>
   </soapenv:Header>
   <soapenv:Body>
      <re:MyRequest>
         <re:MyOrder>
            <re:MyItems>
               <re:ItemCode>TEST</re:ItemCode>
               <re:ItemName>TEST</re:ItemName>
               <re:ItemRef>TEST</re:ItemRef>
            </re:MyItems>
        </re:MyOrder>
      </re:MyRequest>
   </soapenv:Body>
</soapenv:Envelope>

I use Spring Framework's WebServiceTemplate to consume the SOAPService.But when i try it i find that the request object which is the body section goes perfectly.But the header section goes as null.My SOAP client is given below :

SOAPClient :

public Object callWebService(MyRequest request){
    MyResponse myObj = (MyResponse)getWebServiceTemplate().marshalSendAndReceive("http://localhost:9000/MyServices/ws/soap/MyService", request,new WebServiceMessageCallback() {

        public void doWithMessage(WebServiceMessage message) {
            try {
                SoapMessage soapMessage = (SoapMessage)message;
                SoapHeader header = soapMessage.getSoapHeader();
                StringSource headerSource = new StringSource("<MYHeader>\n<Auth>\n<MyID>1312</MyID>\n<Pwd>test213</Pwd>\n</Auth>\n</MYHeader>");
                Transformer transformer = TransformerFactory.newInstance().newTransformer();
                transformer.transform(headerSource, header.getResult());
            } catch (Exception e) {
                System.out.println("ERROR : "+e);
            }
        }
    });
    MyResponse response= (MyResponse) myObj;
    return myObj;
}

In the above the "request" has the body section of the SOAPRequest.Kindly let me know what i am missing here.Thanks in advance

like image 884
Deepak Ramakrishnan Kalidass Avatar asked Jan 27 '23 03:01

Deepak Ramakrishnan Kalidass


2 Answers

From my opinion, using SAAJSoapMessage is simpler and cleaner. Here is doWithMessage example method code. Let me know your thoughts!!.

import org.springframework.ws.soap.saaj.SaajSoapMessage;
import org.springframework.xml.transform.StringSource;

public void doWithMessage(WebServiceMessage message) {
    if (message instanceof SaajSoapMessage) {
        try {
            SaajSoapMessage soapMessage = (SaajSoapMessage) message;
            SoapHeader soapHeader = soapMessage.getSoapHeader();
            StringSource headerSource = new StringSource("<MYHeader>\n<Auth>\n<MyID>1312</MyID>\n<Pwd>test213</Pwd>\n</Auth>\n</MYHeader>");
            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            transformer.transform(headerSource, soapHeader.getResult());
        } catch (TransformerFactoryConfigurationError | TransformerException e) {
            logger.error(e.getMessage(), e);
        }
    }
}
like image 115
Imran Avatar answered Feb 07 '23 13:02

Imran


I was able to figure out the issue.I was able to add header using JAXBContext in order to achieve adding the header.I have modified my Code as given in the link here,and it worked like gem.I am giving the code below for reference :

Code

public Object callWebService(MyRequest request){
    MyResponse myObj = (MyResponse)getWebServiceTemplate().marshalSendAndReceive("http://localhost:9000/MyServices/ws/soap/MyService", request,new WebServiceMessageCallback() {

        public void doWithMessage(WebServiceMessage message) {
            try {
                    SoapHeader soapHeader = ((SoapMessage) message).getSoapHeader();

                    // create the header element
                    ObjectFactory factory = new ObjectFactory();
                    MYHeader headerType = factory.createMYHeader();
                    Auth authType = new Auth();
                    authType.setMyID("1010");
                    authType.setPwd("myPwd");
                    headerType.setAuth(authType);
                    JAXBElement<MYHeader> headers =
                            factory.createMYHeader(headerType);

                    // create a marshaller
                    JAXBContext context = JAXBContext.newInstance(MYHeader.class);
                    Marshaller marshaller = context.createMarshaller();
                    marshaller.marshal(headers, soapHeader.getResult());
            } catch (Exception e) {
                System.out.println("ERROR : "+e);
            }
        }
    });
    MyResponse response= (MyResponse) myObj;
    return myObj;
}
like image 43
Deepak Ramakrishnan Kalidass Avatar answered Feb 07 '23 13:02

Deepak Ramakrishnan Kalidass