Creating a Java Client for the Service

Creating a Java Client for the web Service

Now we will create a Java client which used the services we created now. There are many ways to create clients and I will explain one of them.

 

File -> New -> Project -> Dynamic Web project

 

 

 

 

 

Click Finish and now you have a dynamic web project ?CalculatorServiceClient?.

Now select on your Calculator.java file in CalculatorService project.

 

File -> New -> Other -> Web Service Client

 

 

 

 

Next

 

Set it to Axis 2.

 

 

Text Box: Add service definition URL - http://localhost:8080/CalculatorService/services/Calculator?wsdl

 

Select Calculator client project

 

 

Next

 

Keep the default setting s and press finish. You see that new classes are created for the client.

 

 

 

 

Now we will write a simple console application which uses these generated classes to access the services ? CalculatorServiceClient.java.

 

 

 

 

 

 

 

 

 

 

 

 

 

package org.bpt.services;

 

import org.apache.axis2.AxisFault;

 

public class CalculatorServiceClient {

 

????? public static void main(String[] args) {

??????????? try {

????????????????? CalculatorStub stub = new CalculatorStub();

????????????????? double val = add(stub, 12,13);

????????????????? System.out.println("Got response from add service. result = " +val);

??????????? } catch (AxisFault e) {

????????????????? e.printStackTrace();

??????????? }

 

????? }

 

????? /* fire and forget */

????? public static double add(CalculatorStub stub, double arg1, double arg2) {

??????????? try {

????????????????? CalculatorStub.Add req = new CalculatorStub.Add();

????????????????? req.setArg1(arg1);

????????????????? req.setArg2(arg2);

????????????????? CalculatorStub.AddResponse res = stub.add(req);

?????????????????

????????????????? return res.get_return();

??????????? } catch (Exception e) {

????????????????? e.printStackTrace();

????????????????? System.err.println("\n\n\n");

??????????? }

??????????? return -1;

????? }

 

 

}

 

 
 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Run the application and you see the result in the console. Congratulations. You have created your first web service in eclipse. You are now ready to fly to the heights. All the best for a great fly.

 


--------------------------------------------------------------------------------------------------------

TABLE OF CONTENTS

--------------------------------------------------------------------------------------------------------

Part1 : Introduction - Developing and debugging web services using axis, eclipse and Tomcat
Part2 : Create Web Services using axis and eclipse
Part3 : Check Services
Part4 : Debugging the Web Services created using eclipse
Part5 : Creating a Java Client for the web Service
Leave a comment Discuss about the topic

Related Links