Create a Simple Java Web service Client

In earlier article we have seen how to develop simple web service in PHP. Now we will see how we can call the web services using Java client. We will be using the same wsdl (MathOperationService.wsdl) that we exposed thorough our PHP service. We are going to use Eclipse IDE for our client generation.

First create a simple Java Project in your eclipse and copy the above mentioned wsdl to the project directory. You will be able to see this wsdl in the project navigation of eclipse. Right click on the wsdl. You will see many options, find Web Services and click on that. You will be presented with more options now choose Generate Client. So so far you clicked as shown here –

Create Java Web service Client - Step 1

Create Java Web service Client – Step 1

Once you click on the ‘Generate Client’, it will take you through some simple set up instructions. Choose the next setting as-

Create Java Web service Client - Step 2

Create Java Web service Client – Step 2

You need to provide some package structure when classes required can be generated. Once you provide the package name and click finish, you will see following classes are generated under your project-

Objects Creates by Java Client

Objects Creates by Java Client

Now, you have all the classes that are required to invoke a web service. You only need a java class that will do the service invoking for you. I have prepared one simple java program ‘WSClient.java’. I am going to use pre-generated  java class ‘MathOperationServicePortTypeProxy.java’ to call the one of the web service. You can see the code here-

This java program is preparing inputs (AddRequestType & MultiplyRequestType) as required by web services implementation and invoking the service accordingly. The response is collected in proper return objects (AddResponseType & MultiplyResponseType) and used.

You need to run this as stand-alone Java Program, you will be able to see the proper output in console.

Add a Comment

Your email address will not be published. Required fields are marked *