How to Create a Web Service Client in 3 Steps
With the increasing complexity of using Web services, the method of invoking them has also become increasingly complex. Users are required to learn a lot more than how to pass parameters to a specific url to get results. Learning the required domain is a waste of time if it is only a one time scenario or you don't want to know about Web service specifications at all.
Writing a client manually is a time consuming process when having a very complex invocation procedure. From passing parameters to transportation methods and to applying security, the client has to be prepared before making the house call. Although these necessities are described in the WSDL file, understanding a long and complex WSDL file and converting the requirements into code can take hours specially if you lack the relevant knowledge. This procedure can be automated using the Apache Axis2 code generation libraries. This functionality now has been included as an Eclipse tool (one of the most popular open source IDEs in the world) so that within few seconds you can generate the client stubs according to the WSDL.
The following are the 3 steps,
| 3.4 or higher | |
| Java | 1.5 or higher |
| WSO2 WSAS | 3.1.x |
| eclipse features |
Note: Visit the resources section of this tutorial to find out from where to download the prerequisites or click the above links.
Open the Servers View in Eclipse by Window -> Show View -> Other... and select "Servers"
or just switch to "Java EE" perspective which will have "Server" view by default.
After opening the "Servers" view, if it does not contain a WSAS Server, right click on the view item content area and select New -> Server
Select "WSO2 WSAS 3.1" and click "Next"
Specify the WSAS binary distribution location and click "Finish".
You will see a WSAS server being added. You don't need to start the server to do this tutorial. If you want to start the server click on the WSAS server in the list in Servers view and click the Start button of the view in upper right hand side.
As a first step, you need to get your hands on to the WSDL of the Web service. If it not hosted online, import the wsdl file into the eclipse workspace. In this tutorial, we will be using the WSDL at http://www50.brinkster.com/vbfacileinpt/np.asmx?WSDL. You may use any other valid WSDL.
(At this point if you have imported the WSDL file to the workspace, select the WSDL file from the Project Explorer)
Go to File -> New -> Other... (or Ctrl + N)
Select "Web Service Client" and click "Next"
Specify the WSDL file in the "Service Definition" text box if it is not already there.
Click the "Server" link and select "WSO2 WSAS Server 3.1" among existing servers or server types.
Select "WSO2 Web Services Application Server".
Click "OK"
Click "Next"
In this page, you can customize how the Client Stubs are generated. For now, lets leave the settings as it is. Click "Finish"
The related stubs will be generated in the given Eclipse project.
Inside an existing class or in a new class we'll add the java code to invoke the Web service now. As long as the stub classes are accessible you can use any class in any project in the workspace. I'm creating a new class "PrimeNumberViewer" inside the stub generated project itself ("WebServiceProject") adding the following method.
public static void main(String[] args) throws AxisFault {
Scanner in = new Scanner(System.in);
String input;
String prompt = "\nEnter Number (q - quit):";
PrimeNumbersStub primeNumbersStub = new PrimeNumbersStub();
GetPrimeNumbers primeNumbersParameter=new GetPrimeNumbers();
System.out.print(prompt);
while (!(input=in.nextLine()).equalsIgnoreCase("q")){
try{
int maxNumber = Integer.parseInt(input);
primeNumbersParameter.setMax(maxNumber);
GetPrimeNumbersResponse primeNumbers = primeNumbersStub.getPrimeNumbers(primeNumbersParameter);
System.out.println(" Prime Numbers: "+primeNumbers.getGetPrimeNumbersResult());
}catch (Exception e){
System.out.println("ERROR: "+e.getMessage());
}
System.out.print(prompt);
}
}
Only the bolded text/commands in the above script are related to the web service invocation. Rest are just there to create a fancy program.
Now just execute the above method. In my case I'll just execute it as a java class.
eg: primeNumbersStub.getPrimeNumbers(....)
eg: primeNumbersStub.getPrimeNumbers(primeNumbersParameter);
here the primeNumbersParameter is an object which keeps the required parameters. Normally it would contain getter/setter methods for the parameters (primeNumbersParameter.setMax(maxNumber);)
eg: primeNumbers.getGetPrimeNumbersResult()
It is easier to create a Web service rather than finding issues related to it. But these tools help you in debugging your Web service just like debugging your Java application in Eclipse.
Saminda Wijeratne, Senior Software Engineer, WSO2, samindaw@wso2.com