Monday, May 24, 2010

Web service client using eclipse axis2 plugin

Few months ago I blog how to deploy a web service and today I am going to create a client for that web service.
Here is the java class which I am going to deploy as the service.

package WebServices;

/**
* @author Eranda
*
*/
public class HelloService {
public String sayHello(String name){
return "Hello "+name;
}

}

This we service take a "name" and return Hello name.

The following things should be done after deploying the service to verify it's existance.

Go to your browser and check whether your web service is deployed or not. Use http://localhost:8080/ProjectName/services/listServices to check it. If it doesn't show the web service list then you have to deploy your service again properly.

Here is the page you have to have.



Now click on the service name of what you deployed. It will show you the wsdl of the web service. Now copy the URL of the wsdl.

Then go to eclipse  and then select File->new->Other->Web Service Client and click Next.

You can see a window like follows,


Copy the wsdl URL to Service Definition textbox.
Set the slider to Start Client.
Then click Server link and see whether the server is selected and set Web service runtime to axis2.



Click OK
Then goto Client project: Axis2 link and set the Web service client name as your preference or leave it as it is.


Click OK
And then click Next

Then the project will create and the following window appears.


Set the parameters with your values or you may use default.
Check the box Generate a JUnit test case to test the service and click Finish and the web service stub and the test case will create after that.

Here is the look of the project after doing that.


To test the web service you can use the test case which create automatically. To do this right click on the test class (here it is HelloServiceTest.java) Run As->JUnit Test
If all the test are successful then your client side is working correctly. Later we will see how to write a class to invoke the web service methods and get results.

PS: Some times you may get Axis faults. This exception throws because of  the wrong server host in stub. By correcting the stub(there are 2 places to correct) with the correct host name you can make it correct. (This host change thing mostly occurred because of the dynamic IP of the internet connections.)
Search Amazon.com for web services java

No comments:

Post a Comment