Hi Guys,
Today, I am going to show that how you can consume a SOAP WS using SAAJ framework. SAAJ is shipped with JSE 1.6 and above.
What is SAAJ?
SOAP with Attachments API for Java (SAAJ) is mainly used for dealing directly with SOAP Request/Response messages which happens behind the scenes in any Web Service API. It allows the developers to directly send and receive soap messages instead of using JAX-WS.
To know more about SAAJ framework, please check official documentation – http://docs.oracle.com/javaee/5/tutorial/doc/bnbhg.html
I am going to use an open service available at http://www.webservicex.net/. We will be calling Geo IP method. This method is for fetching the country name and its code based on the IP address supplied as input. . You can get the complete WSDL here – http://www.webservicex.net/geoipservice.asmx?WSDL.
First of all, you need to build the SOAP request message that we need to send. To know how the SOAP request should like, you can used Soap UI tool. The required SOAP request message for GeoIP looks like below:
<soapenv:envelope xmlns:web="http://www.webservicex.net/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:header> </soapenv:header> <soapenv:body> <web:getgeoip> <web:ipaddress>172.168.0.45</web:ipaddress> </web:getgeoip> </soapenv:body> </soapenv:envelope>
Now let’s see how you need to construct this message using SAAJ framework and send the request: SAAJSoapClient.java
import javax.xml.soap.*; public class SAAJSoapClient { public static void main(String args[]) throws Exception { // Create SOAP Connection SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); SOAPConnection soapConnection = soapConnectionFactory.createConnection(); // Send SOAP Message to SOAP Server String wsdlURL = "http://www.webservicex.net/geoipservice.asmx?WSDL"; SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), wsdlURL); // print SOAP Response System.out.print("Response SOAP Message:"); soapResponse.writeTo(System.out); soapConnection.close(); } /** * Creat SOAP Request message * * Construct SOAP Request Message: <soapenv:envelope xmlns:web="http://www.webservicex.net/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:header></soapenv:header> <soapenv:body> <web:getgeoip> <web:ipaddress>172.168.0.45</web:ipaddress> </web:getgeoip> </soapenv:body> </soapenv:envelope> * @return * @throws Exception */ private static SOAPMessage createSOAPRequest() throws Exception { MessageFactory messageFactory = MessageFactory.newInstance(); SOAPMessage soapMessage = messageFactory.createMessage(); SOAPPart soapPart = soapMessage.getSOAPPart(); String serverURI = "http://www.webservicex.net/"; /** * Construct SOAP Envelope * <soapenv:envelope xmlns:web="http://www.webservicex.net/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"></soapenv:envelope> * */ SOAPEnvelope envelope = soapPart.getEnvelope(); envelope.addNamespaceDeclaration("web", serverURI); /** * Construct SOAP Body * * <soapenv:body> <web:getgeoip> <web:ipaddress>172.168.0.45</web:ipaddress> </web:getgeoip> </soapenv:body> * */ SOAPBody soapBody = envelope.getBody(); SOAPElement soapBodyElem = soapBody.addChildElement("GetGeoIP", "web"); SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("IPAddress", "web"); soapBodyElem1.addTextNode("172.168.0.45"); /** * Construct SOAP Header * * <soapenv:header></soapenv:header> */ MimeHeaders headers = soapMessage.getMimeHeaders(); headers.addHeader("SOAPAction", serverURI + "GetGeoIP"); soapMessage.saveChanges(); /* Print the request message */ System.out.print("Request SOAP Message:"); soapMessage.writeTo(System.out); System.out.println(); return soapMessage; } }
Execute it. You will see SOAP response message in your console.
<soap:envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:body> <getgeoipresponse xmlns="http://www.webservicex.net/"> <getgeoipresult> <returncode>1</returncode> <ip>172.168.0.45</ip> <returncodedetails>Success</returncodedetails> <countryname>United States</countryname> <countrycode>USA</countrycode> </getgeoipresult> </getgeoipresponse> </soap:body> </soap:envelope>
That is it. Easy isn’t it? Hope you guys likes it.
Soon, I will create a generic class for SOAP WS consumption based on SAAJ framework and will upload it on GitHub. Just stay tuned.
That’s it guys for today. Critics/suggestions are very much welcome.
Have a nice day ahead.
Cheers!
Hi Nitesh,
It is really very useful and easy to understand. Have you also created a generic class for SOAP WS consumption based on SAAJ framework??
Thanks
Sharad
Hi Sharad,
I have not yet. But will do it soon.
You too can try it. Basic design principle – a method accepting exactly n number of parameters and returning one value.
(Honestly, thanks a lot for reminding me. ;-))
Thanks
With Regards
Nitesh Apte
Hello admin
I am new to this, as we see the header tag in the request is empty, then why are we using statement
headers.addHeader(“SOAPAction”, serverURI + “GetGeoIP”);
why we are not simply creating a header and let it be empty whereas it is empty in request.????
Hi Sandeep,
The code you have mentioned is for adding MimeHeader. That’s not soap header. The reason I did that (though I don’t remember accurately as I wrote this article more than a year back.) is because I guess, for the service which I used for consuming in example. If you remove that part of code Request XML will be generated but you won’t get the response.
I will correct my comment in code. Thanks for pointing that out.
Thanks