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 :- http://www.concretepage.com/webservices/java-saaj-web-service-example to acheive it... My SOAP request is as follow :-
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="http://services.test.com/schema/MainData/V1">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<v1:retrieveDataRequest>
<v1:Id>22</v1:Id>
</v1:retrieveDataRequest>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
and my SOAP response is :
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<retrieveDataResponse xmlns="http://services.test.com/schema/MainData/V1">
<Response>The Data retrieved from the Database</Response>
<Id>21</Id>
<Name>fdfdf</Name>
<Age>44</Age>
<Designation>dgdgdfg</Designation>
</retrieveDataResponse>
</soap:Body>
</soap:Envelope>
and my Java code is :-
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:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="http://services.test.com/schema/MainData/V1">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<v1:retrieveDataRequest>
<v1:Id>21</v1:Id>
</v1:retrieveDataRequest>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
*/
// 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();
}
}
}
Now my issue is I am getting the SOAP request printed in console but I and not getting the SOAP response back from the service ... Please help ... How to get the SOAP response printed in console ... I am not able to get response
Can you not just try this to capture the response?
SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);
ByteArrayOutputStream out = new ByteArrayOutputStream();
msg.writeTo(out);
String strMsg = new String(out.toByteArray());