Advertisements

SOAP Client in java

If you are looking for java client for RESTful web service then you should visit this article:

Simple REST client in java

This article will teach you how to create a SOAP client in java. That is creating a client in java which requests soap server (no need to be in java) and get response from it. First create request message as follows:

SOAPMessage message = MessageFactory.newInstance().createMessage();
SOAPHeader header = message.getSOAPHeader();
header.detachNode();

If you have namespace required in SOAP request then add those namespaces to envelope element as follows:

SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
envelope.setAttribute("namspace","namespaceUrl");

You can add as many attribute as you want. Now time to create request message body.
Following body is made assuming that SOAP server where this client will connect will have a public service method called getResponse(name) available.

SOAPBody body = message.getSOAPBody();
QName bodyName = new QName("getResponse");
SOAPBodyElement bodyElement = body.addBodyElement(bodyName);
SOAPElement symbol = bodyElement.addChildElement("name");
symbol.addTextNode(“Harry joy”);

Now that request message is ready it’s time to connect to soap server and send request. Following code will do this:

SOAPConnection connection = SOAPConnectionFactory.newInstance().createConnection();
SOAPMessage response = connection.call(message, endpoint);
connection.close();

In above code endpoint is the SOAP server URL without “?wsdl”. To parse response you can do as follows:

SOAPBody responseBody = response.getSOAPBody();
SOAPBodyElement responseElement = (SOAPBodyElement)responseBody.getChildElements().next();
SOAPElement returnElement = (SOAPElement)responseElement.getChildElements().next();
if(responseBody.getFault() != null) { //-- If response has any fault.
 	System.out.println(returnElement.getValue()+" "+responseBody.getFault().getFaultString());
}  else  {
   	System.out.println(returnElement.getValue());
}

Here request/response messages are totally dependent on SOAP server, how you have configured it.


Bonus: How to print request/response xml?

String getXmlFromSOAPMessage(SOAPMessage msg) throws SOAPException, IOException {
	ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
	msg.writeTo(byteArrayOS);
	return new String(byteArrayOS.toByteArray());
}

Use:

System.out.println(getXmlFromSOAPMessage(message));
System.out.println(getXmlFromSOAPMessage(response));

Full source code:

public class SOAPClient {

	private static final String endpoint = "http://localhost/SOAPService/MySoapService";

	public static void main(String[] args) throws SOAPException {
		SOAPMessage message = MessageFactory.newInstance().createMessage();
		SOAPHeader header = message.getSOAPHeader();
		header.detachNode();

		SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
		envelope.setAttribute("namespace","namespaceUrl");

		SOAPBody body = message.getSOAPBody();
		QName bodyName = new QName("getResponse");
		SOAPBodyElement bodyElement = body.addBodyElement(bodyName);
		SOAPElement symbol = bodyElement.addChildElement("name");
		symbol.addTextNode("Harry Joy");

		SOAPConnection connection = SOAPConnectionFactory.newInstance().createConnection();
		SOAPMessage response = connection.call(message, endpoint);
		connection.close();

		SOAPBody responseBody = response.getSOAPBody();
		SOAPBodyElement responseElement = (SOAPBodyElement)responseBody.getChildElements().next();
		SOAPElement returnElement = (SOAPElement)responseElement.getChildElements().next();
		if(responseBody.getFault()!=null){
			System.out.println(returnElement.getValue()+" "+responseBody.getFault().getFaultString());
		} else {
			System.out.println(returnElement.getValue());
		}

		try {
			System.out.println(getXmlFromSOAPMessage(message));
			System.out.println(getXmlFromSOAPMessage(response));
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	private static String getXmlFromSOAPMessage(SOAPMessage msg) throws SOAPException, IOException {
		ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
		msg.writeTo(byteArrayOS);
		return new String(byteArrayOS.toByteArray());
	}

}
Advertisements
%d bloggers like this: