title: Not able to get SOAP response after calling external webservice from java
body: <p>I have a requrement of calling an external webservice from java where I will where I will create the SOAP request and pass to the webservice and will get the SOAP response back ... I went through the following url :- <a href="http://www.concretepage.com/webservices/java-saaj-web-service-example" rel="nofollow">http://www.concretepage.com/webservices/java-saaj-web-service-example</a>    to acheive it... My <strong>SOAP request</strong> is as follow :-</p>
  
  <pre><code>&lt;SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="http://services.test.com/schema/MainData/V1"&gt;
     &lt;SOAP-ENV:Header/&gt;
     &lt;SOAP-ENV:Body&gt;
        &lt;v1:retrieveDataRequest&gt;
           &lt;v1:Id&gt;22&lt;/v1:Id&gt;
        &lt;/v1:retrieveDataRequest&gt;
     &lt;/SOAP-ENV:Body&gt;
  &lt;/SOAP-ENV:Envelope&gt;
  </code></pre>
  
  <p>and my <strong>SOAP response</strong> is :</p>
  
  <pre><code>&lt;soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"&gt;
     &lt;soap:Body&gt;
        &lt;retrieveDataResponse xmlns="http://services.test.com/schema/MainData/V1"&gt;
           &lt;Response&gt;The Data retrieved from the Database&lt;/Response&gt;
           &lt;Id&gt;21&lt;/Id&gt;
           &lt;Name&gt;fdfdf&lt;/Name&gt;
           &lt;Age&gt;44&lt;/Age&gt;
           &lt;Designation&gt;dgdgdfg&lt;/Designation&gt;
        &lt;/retrieveDataResponse&gt;
     &lt;/soap:Body&gt;
  &lt;/soap:Envelope&gt;
  </code></pre>
  
  <p>and my <strong>Java code</strong> is :-</p>
  
  <pre><code>import javax.xml.soap.*;
  import javax.xml.transform.Source;
  import javax.xml.transform.Transformer;
  import javax.xml.transform.TransformerFactory;
  import javax.xml.transform.stream.StreamResult;
  
  public class Main2
  {
          /**
           * Method used to create the SOAP Request
           */
          private static SOAPMessage createSOAPRequest() throws Exception
          {
                  MessageFactory messageFactory = MessageFactory.newInstance();
                  SOAPMessage soapMessage = messageFactory.createMessage();
                  SOAPPart soapPart = soapMessage.getSOAPPart();
  
                  /*
                  Construct SOAP Request Message:
                  &lt;SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="http://services.test.com/schema/MainData/V1"&gt;
     &lt;SOAP-ENV:Header/&gt;
     &lt;SOAP-ENV:Body&gt;
        &lt;v1:retrieveDataRequest&gt;
           &lt;v1:Id&gt;21&lt;/v1:Id&gt;
        &lt;/v1:retrieveDataRequest&gt;
     &lt;/SOAP-ENV:Body&gt;
  &lt;/SOAP-ENV:Envelope&gt;
                   */
  
                  // SOAP Envelope
                  SOAPEnvelope envelope = soapPart.getEnvelope();
                  envelope.addNamespaceDeclaration("v1", "http://services.test.com/schema/MainData/V1");
  
                  // SOAP Body
                  SOAPBody soapBody = envelope.getBody();
                  SOAPElement soapBodyElem = soapBody.addChildElement("retrieveDataRequest", "v1");
                  SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("Id", "v1");
                  soapBodyElem1.addTextNode("21");
  
                  MimeHeaders headers = soapMessage.getMimeHeaders();
                  headers.addHeader("SOAPAction", "http://services.test.com/schema/MainData/V1"  + "http://services.test.com/schema/MainData/V1/retrieveDataOperation");
  
                  soapMessage.saveChanges();
  
                  // Check the input
                  System.out.println("Request SOAP Message = ");
                  soapMessage.writeTo(System.out);
                  System.out.println();
                  return soapMessage;
          }
  
          /**
           * Method used to print the SOAP Response
           */
          private static void printSOAPResponse(SOAPMessage soapResponse) throws Exception
          {
                  TransformerFactory transformerFactory = TransformerFactory.newInstance();
                  Transformer transformer = transformerFactory.newTransformer();
                  Source sourceContent = soapResponse.getSOAPPart().getContent();
                  System.out.println("\nResponse SOAP Message = ");
                  StreamResult result = new StreamResult(System.out);
                  transformer.transform(sourceContent, result);
          }
  
          /**
           * Starting point for the SAAJ - SOAP Client Testing
           */
          public static void main(String args[])
          {
                  try
                  {
                           // Create SOAP Connection
                          SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
                          SOAPConnection soapConnection = soapConnectionFactory.createConnection();
  
                          //Send SOAP Message to SOAP Server
                          String url = "http://localhost:8082/mainData?wsdl";
                          SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);
  
                          // Process the SOAP Response
                          printSOAPResponse(soapResponse);
  
                          soapConnection.close();
                  }
                  catch (Exception e)
                  {
                          System.err.println("Error occurred while sending SOAP Request to Server");
                          e.printStackTrace();
                  }
          }
  }
  </code></pre>
  
  <p>Now my issue is I am getting the <strong>SOAP request</strong> printed in console but I and not getting the <strong>SOAP response</strong> back from the service ... Please help ... How to get the SOAP response printed in console ... I am not able to get response</p>