1. Create java web service in Eclipse using Axis2 (Part 01)
2. Create java web service in Eclipse using Axis2 (Part 02)
Before implementing the client first we have to find the wsdl file.
First run the created project on Tomcat server. To do this right click on project select Run As then select Run on Server.
Click on Services
Select Hello which is your web service class name. Under Available Operations you can see your web service operation.
After clicking on Hello you can see the WSDL file. Copy WSDL link to the clip board.
OK. We coppied the WSDL link.
Now we have to create the web service client.
Right click the web service class select New >> Other >> Web Services >> Web service Client.
Click Next. Paste link of the wsdl file to the Service definition box. (If you want to access the wsdl in another computer via a net work replace "localhost" in the WSDL url with the ip address of the server machine)
Make sure that the Server runtime is Tomcat & the Web service runtime is Axis2 by clicking the links underlined red color in the following diagram.
Client type : Java Proxy
Click finish to generate Callback Handler & the Stub in same package where the web service class exists.(If you want to change the client package click next & give a package name to Custom package name as you wish)
Then create new class. Class name that I used is Client. Here is the project structure.
Following code shows how we can invoke the web service method using auto generated Stub. Paste this code to newly created class. This client class also in the same package with web service class.
package org.webapp.ws; import org.webapp.ws.HelloStub.SayHello; public class Client { public static void main(String[] args) throws Exception { HelloStub stub = new HelloStub(); //Creating the request org.webapp.ws.HelloStub.SayHello request = new org.webapp.ws.HelloStub.SayHello(); request.setName("Java web"); //Invoking the service org.webapp.ws.HelloStub.SayHelloResponse response = stub.sayHello(request); System.out.println("Response : " + response.get_return()); } }
Right click Client class and run the client as a java application.
Result :
Click here to download the complete project with client.
If you find this post helpful don't forget to leave a comment. Your comments always encourage me to write more!
Happy coding!