SOAP : Basic Terminology and Writing a Webservice Client

Web Service Description Language(WSDL) is a language for describing web services and how to access them. WSDL is written in XML.  We will learn more about wsdl later on, but for now keep in mind that a wsdl is a type of contract that is shared with the webservice consumer by the webservice creators. How its implemented on either side is entirely up to them.

Universal Description, Discovery and Integration (UDDI) is a directory service where businesses can register and search for Web services. Although its not used that much as mostly there is a direct communication between the webservice consumer and creators, but still you might want to find some publically deployed service.

SOAP stands for Simple Object Access Protocol. SOAP is a protocol for accessing web services. SOAP is based on XML. SOAP provides a way to communicate between applications running on different operating systems, with different technologies and programming languages. A SOAP message is an ordinary XML document containing the following elements:

  • An Envelope element that identifies the XML document as a SOAP message
  • A Header element that contains header information
  • A Body element that contains call and response information
  • A Fault element containing errors and status information

Writing a Webservice Client

Now lets first consume a webservice before buiding one, i.e write a client that uses a exsisting webservice, makes a call to that web service and retrive the result. First step is to search a web service for out need. I happen to know a free working webservice thats avaiable on net called – GeoIPService. You can google it or search any other webservice of your choice and follow the steps accordingly.

GeoIPService – enables you to easily look up countries by IP address / Context. Note that it might be this webservice may not be working when you are trying, just search another one. In the home page itself you will find a WSDL Schema Location – http://www.webservicex.net/geoipservice.asmx?WSDL

Open this url in a new tab and you will see the whole wsdl. Save this in a wsdl file (for eg. geoipservice.wsdl).

In Eclipse, create a dynamic web project and copy this wsdl in WebContent. Now right click and generate the client of it as show below:

GenerateClient1

 

GenerateClient2

 

Now click finish. You will notice a package has been made automatically in your src folder.

GenerateClient3

 

You should not write any code in this package, Its not mandatory but a good practice. Lets write our own code in a different package:


package mynotes.webservice.client;

import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;

import javax.xml.rpc.Service;

import net.webservicex.www.GeoIP;
import net.webservicex.www.GeoIPServiceSoapStub;

public class TestGeoIpService {

public static void main(String[] args) throws RemoteException, MalformedURLException {
 System.out.println("START");
 Service service=null;
 URL endpointURL =new URL("http://www.webservicex.net/geoipservice.asmx");
 GeoIPServiceSoapStub aGeoIPServiceSoapStub=new GeoIPServiceSoapStub(endpointURL , service);
 GeoIP myGeoIP =aGeoIPServiceSoapStub.getGeoIP("74.125.236.145");
 System.out.println("WebService Result");
 System.out.println("getCountryName=>"+myGeoIP.getCountryName()+"\n"+
 "getCountryCode=>"+myGeoIP.getCountryCode()+"\n"+
 "getIP=>"+myGeoIP.getIP());
 System.out.println("END");
 }

}

Make sure your imports are correct. Output:


START
WebService Result
getCountryName=>United States
getCountryCode=>USA
getIP=>74.125.236.145
END

<span style="line-height: 1.5em;">

74.125.236.145 is my input which I found by pinging http://www.google.com, and the webservice returned the country name where its hosted. For any webservice call we need to make an object of stub. Notice that GeoIPServiceSoapStub class was created when you gegnerated the client. Most cases we use the constructor that takes a endpoint URL and a Service parameter. The endpoint URL is where the service is hosted. You can get it on the wsdl itself in most cases inside the wsdl:port tag like in here if you open the wsdl at the end there is – <soap:address location=”http://www.webservicex.net/geoipservice.asmx”/&gt; inside <wsdl:port> tag.
Note that wsdl may not always contain the corrent endpoint url, like although the wsdl is same the endpoint url will change for development and production instance of the service.
After creating the stub object, we need to call the relevant method of our requirement. In most cases a documention of all the method is provided by the webservice creator.

Also, make a note of it that its not necessary that the client be a dynamic web project, it could be a normal java project also. As a best practice the package that was auto created when we did a generate client on the wsdl, could be kept as a jar instead. wsdl is not actually required by your client code.

Grab all the code from my github repository.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: