Java SAAJ Web Service Example
January 05, 2014
SAAJ is SOAP with Attachments API for Java. With the help of SAAJ, a developer can produce and consume messages conforming to SOAP. This page will describe how to use SAAJ in our application. In our demo we will learn to create soap request, send it and get the response.
How to create SOAP Request Using SAAJ
To create Soap request different classes of javax.xml.soap is used. Find the short definition of those classes.MessageFactory
MessageFactory is factory to create SOAPMessage instance. To get instance of SOAPMessage, call newInstance() method as belowMessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage
SOAPMessage is an XML document or MIME message. The first body part of SOAPMessage is XML or SOAP document. SOAPMessage is created asSOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart
SOAPPart is a container for SOAP specific portion of SOAPMessage. SOAPPart object is MIME part. To create SOAPPart, call getSOAPPart() method of SOAPMessage as belowSOAPPart soapPart = soapMessage.getSOAPPart();
SOAPEnvelope
SOAPEnvelope is the container for SOAPHeader and SOAPBody portions. SOAPEnvelope is created by SOAPPart as belowSOAPEnvelope soapEnvelope = soapPart.getEnvelope();
SOAPBody
SOAPBody is an object for SOAP body for a SOAP message. SOAPBody is created by SOAPEnvelope as below.SOAPBody soapBody = soapEnvelope.getBody();
SOAPElement
SOAPElement represents the element of SOAPMessage. SOAPElement is created by SOAPBody as belowSOAPElement soapElement = soapBody.addChildElement("getWelcomeMsg", "end");
How to Send SOAP Request to Web Service
To send soap request to server, we need to create a connection with remote entity. Different classes are used to create connection which is given below.SOAPConnectionFactory
SOAPConnectionFactory is a factory to create SOAPConnection. To get instance of SOAPConnectionFactory, call newInstance() method of this class.SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection
SOAPConnection is a point to point connection that sends the message directly to remote entity. To instantiate SOAPConnection, createConnection() method of SOAPConnectionFactory is called as belowSOAPConnection soapConnection = soapConnectionFactory.createConnection();
How to create SOAP Response Using SAAJ
To get soap response, below API are used.TransformerFactory
TransformerFactory is a factory to create Transformer. TransformerFactory is created as belowTransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer
Transformer can transform a source tree into result tree. Transformer is created as belowTransformer transformer = transformerFactory.newTransformer();
Source
The instance of this class acts as XML source and is created asSource sourceContent = soapResponse.getSOAPPart().getContent();
StreamResult
It is a holder of transformer result that can be an XML. To print the result in console we do likeStreamResult result = new StreamResult(System.out); transformer.transform(sourceContent, result);
SAAJClientDemo.java
package com.concretepage.client; import javax.xml.soap.MessageFactory; import javax.xml.soap.SOAPBody; import javax.xml.soap.SOAPConnection; import javax.xml.soap.SOAPConnectionFactory; import javax.xml.soap.SOAPElement; import javax.xml.soap.SOAPEnvelope; import javax.xml.soap.SOAPMessage; import javax.xml.soap.SOAPPart; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamResult; public class SAAJClientDemo { private static SOAPMessage createSoapRequest() throws Exception{ MessageFactory messageFactory = MessageFactory.newInstance(); SOAPMessage soapMessage = messageFactory.createMessage(); SOAPPart soapPart = soapMessage.getSOAPPart(); SOAPEnvelope soapEnvelope = soapPart.getEnvelope(); soapEnvelope.addNamespaceDeclaration("end", "http://endpoint.concretepage.com/"); SOAPBody soapBody = soapEnvelope.getBody(); SOAPElement soapElement = soapBody.addChildElement("getWelcomeMsg", "end"); SOAPElement element1 = soapElement.addChildElement("arg0"); element1.addTextNode("EveryOne"); soapMessage.saveChanges(); System.out.println("----------SOAP Request------------"); soapMessage.writeTo(System.out); return soapMessage; } private static void createSoapResponse(SOAPMessage soapResponse) throws Exception { TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); Source sourceContent = soapResponse.getSOAPPart().getContent(); System.out.println("\n----------SOAP Response-----------"); StreamResult result = new StreamResult(System.out); transformer.transform(sourceContent, result); } public static void main(String args[]){ try{ SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); SOAPConnection soapConnection = soapConnectionFactory.createConnection(); String url = "http://localhost:8080/WebServiceDemo/Welcome?wsdl"; SOAPMessage soapRequest = createSoapRequest(); //hit soapRequest to the server to get response SOAPMessage soapResponse = soapConnection.call(soapRequest, url); createSoapResponse(soapResponse); soapConnection.close(); }catch (Exception e) { e.printStackTrace(); } } }
----------SOAP Request------------ <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:end="http://endpoint.concretepage.com/"> <SOAP-ENV:Header/> <SOAP-ENV:Body> <end:getWelcomeMsg> <arg0>EveryOne</arg0> </end:getWelcomeMsg> </SOAP-ENV:Body> </SOAP-ENV:Envelope> ----------SOAP Response----------- <?xml version="1.0" encoding="UTF-8"?> <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"> <S:Body> <ns2:getWelcomeMsgResponse xmlns:ns2="http://endpoint.concretepage.com/"> <return>Welcome EveryOne</return> </ns2:getWelcomeMsgResponse> </S:Body> </S:Envelope>