Wednesday, August 15, 2012

SOAP Client in java

SOAP 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:
1SOAPMessage message = MessageFactory.newInstance().createMessage();
2SOAPHeader header = message.getSOAPHeader();
3header.detachNode();
If you have namespace required in SOAP request then add those namespaces to envelope element as follows:
1SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
2envelope.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.
1SOAPBody body = message.getSOAPBody();
2QName bodyName = new QName("getResponse");
3SOAPBodyElement bodyElement = body.addBodyElement(bodyName);
4SOAPElement symbol = bodyElement.addChildElement("name");
5symbol.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:
1SOAPConnection connection = SOAPConnectionFactory.newInstance().createConnection();
2SOAPMessage response = connection.call(message, endpoint);
3connection.close();
In above code endpoint is the SOAP server URL without “?wsdl”. To parse response you can do as follows:
1SOAPBody responseBody = response.getSOAPBody();
2SOAPBodyElement responseElement = (SOAPBodyElement)responseBody.getChildElements().next();
3SOAPElement returnElement = (SOAPElement)responseElement.getChildElements().next();
4if(responseBody.getFault() != null) { //-- If response has any fault.
5    System.out.println(returnElement.getValue()+" "+responseBody.getFault().getFaultString());
6else  {
7    System.out.println(returnElement.getValue());
8}
Here request/response messages are totally dependent on SOAP server, how you have configured it.

Bonus: How to print request/response xml?
1String getXmlFromSOAPMessage(SOAPMessage msg) throws SOAPException, IOException {
2    ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
3    msg.writeTo(byteArrayOS);
4    return new String(byteArrayOS.toByteArray());
5}
Use:
1System.out.println(getXmlFromSOAPMessage(message));
2System.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());
 }

}

No comments:

Post a Comment