Writing a Web Service Client in Java using Eclipse IDE

The steps involved in writing the Web Service Client will be as follows:

a) Locate the service description in the form of WSDL for the web service in question. (Actually the first step is to search UDDI for the services. I am excluding this step here to simplify the process )

b) Once we have the WSDL document, most of the client code is auto-generated using the wsimport tool.

c)  Run the client and invoke the web service.

Now I will demonstrate the steps to write a Web Service client in Java using Eclipse IDE. This web service client is invoking the web services available at the url  "http://www.webservicex.net

        1)      Go to the url : http://www.webservicex.net/ws/default.aspx
      

















      2)      Choose the web service you want to write the client for. I have chosen Computer Unit converter, which gives me the conversion from bytes to kilobytes, megabytes etc.       




























3) The WSDL schema location is mentioned on the page. I can go to this url and have a look at the WSDL.





























4)The values worth noting here are the wsdl service name and the wsdl port name, that we will need for invoking the web service.



 
 5)      Now we will start setting up our eclipse project. Create a new Java Project. Here in this example , we have given it the name ComputerUnitConvertor



     6)      The structure looks like the following :



7)  Open the Windows powershell or command prompt and go to the src directory of the project.

    8)      Now, the stub has to be generated using the wsimport tool available with Java. For that, we use the below command :
Here , wsimport is the command ; -keep is the switch that is used if we want to retain the java files generated. If this switch is not used, then only the class files will be retained. Next , we give the directory name where the java files have to be retained. I want it to be there in src directory, so I gave “.”  as the folder name.  Lastly, we need to specify the wsdl schema location. The wsdl will be read and corresponding client code generated.













 9)      The following is the folder structure and files created after the above is run:



















   10)      On refreshing the eclipse project, it gets reflected as follows:


































11)  Now that the stubs are generated, I can write a test client to invoke the web service. So, I create a new class TestComputerUnitConvertor with a main method in a new package.


































    12)      Following is the code to invoke the web service. The comments describe what is being done in each step. 


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
package in.blogspot.pallavisonal;

import net.webservicex.ComputerUnit;
import net.webservicex.ComputerUnitSoap;
import net.webservicex.Computers;

public class TestComputerUnitConvertor {

 public static void main(String[] args) {
  // Use the WSDL Service name - this provides the implementation of the interface to invoke the web service
  ComputerUnit cunit= new ComputerUnit();
  //WSDL port name - this is for sending the SOAP request to the web service host and getting the response
  ComputerUnitSoap cusoap=cunit.getComputerUnitSoap();
  double convertedUnits=cusoap.changeComputerUnit(10.0, Computers.MEGABYTE, Computers.KILOBYTE);
  //Print the results
  System.out.println("10 MB is equal to "+convertedUnits +" KB");
 }

}

13) The following is the output:

This is all for creating a web service client in Java. Next we will look at how to create a web service in java and deploy it in Tomcat web server.

Comments

Popular posts from this blog

Day#3 of Java Programming Practice- Longest word of a Sentence

Book Review: Those Pricey Thakur Girls

Day# 5 of Java Programming Practice- Change the first letter of each word in a sentence to Uppercase.