Tutorial: How to create Axis2 web service with IntelliJ IDEA? Part 2: Client side

In this post we will create client project for Axis2 service.

Step 1. Create project
Just create simple console project

1

Add Axis2 library to project as described in previous post.

Step 2. Generate client side files
Create build.xml file in the root of the project:
<project name="ForAnt" basedir="." default="generate.client">

 <property environment="env"/>
 <property name="build.dir" value="build"/>

 <path id="axis2.classpath">
  <fileset dir="PATH_TO_AXIS2_LIB">
   <include name="*.jar"/>
  </fileset>
 </path>

 <target name="generate.client">
  <taskdef name="wsdl2java"
     classname="org.apache.axis2.tool.ant.AntCodegenTask"
     classpathref="axis2.classpath"/>
  <wsdl2java
    wsdlfilename="http://localhost:8080/axis2/services/CustomService?wsdl"
    output="${build.dir}/resources" />
 </target>

 <target name="clean">
  <delete dir="${build.dir}"/>
 </target>
</project>

Replace PATH_TO_AXIS2_LIB with real value. Change wsdlfilename attribute value if service WSDL URL is different from mine.

Set build.xml in “Ant Build” and run generate.client. 2 classes should be generated as in screenshot

1-2

Copy package generated in build/resources/src to /src folder.

Step 2. Write code
Create CustomeClient class in src directory. Write to class:
public class CustomClient {
 
 private final static String END_POINT = "http://localhost:8080/axis2/services/CustomService?wsdl";

 public static void main(String[] args) throws RemoteException {

  // create worker. We will send this object
  CustomServiceStub.Worker worker = new CustomServiceStub.Worker();
  worker.setName("Doszhan");
  worker.setSurname("Kalibek");
  worker.setPosition("specialist");
  worker.setSalary(100);

  // initialize stub, set worker as sending object
  CustomServiceStub stub = new CustomServiceStub(END_POINT);
  CustomServiceStub.GetWorkerWithChangedSalary workerOperation = new CustomServiceStub.GetWorkerWithChangedSalary();
  workerOperation.setWorker(worker);

  // send object and receive response
  CustomServiceStub.GetWorkerWithChangedSalaryResponse response = stub.getWorkerWithChangedSalary(workerOperation);
  CustomServiceStub.Worker returnedWorker = response.get_return();

  // print worker to see that salary was changed
  System.out.println("Name: " + returnedWorker.getName());
  System.out.println("Surname: " + returnedWorker.getSurname());
  System.out.println("Position: " + returnedWorker.getPosition());
  System.out.println("Salary: " + returnedWorker.getSalary());

 }
}

Step 3. Run
Run CustomClient class as console application. Be sure that service created in previous post is running. If you see in console:

3

then everything is OK.

If you have found a spelling error, please, notify us by selecting that text and pressing Ctrl+Enter.

Profile photo of Doszhan Kalibek

Doszhan Kalibek