Java SOAP Webservice using Axis 2 and Tomcat Tutorial with examples

Web services are application components which communicate using open protocols. Using Web Services we can publish our application's functions to everyone. This tutorial provides step by step instructions to develop Web Services using Axis2 Web Services / SOAP / WSDL engine and Eclipse IDE. Let's start.

Axis 2

Axis 2 is a web service/SOAP/WSDL engine provided by Apache. It is a java based implementation. Axis 2 provides complete object model and modular architecture. Using Axis 2 you can easily create a web service from a plain java class, send SOAP messages, receive SOAP message.
We have used below tools for this tutorials
  1. Apache Tomcat 7.05.55 Download from this website http://tomcat.apache.org/
  2. axis2-1.6.2 Download from this website http://axis.apache.org/axis2/java/core/
  3. Eclipse IDE - Kepler for Web Developers. Download from this website http://www.eclipse.org/

Set Axis runtime in Eclipse

  • Go to Window Menu.
  • Click on Preferences.
  • Expand Web Services option.
  • Click Axis2 Preferences.
  • Give your axis2 location in the Axis2 runtime location box.
Set Axis Runtime in Eclipse

Set Tomcat runtime in Eclipse

  • Go to Window Menu.
  • Click on Preferences.
  • Expand Server option.
  • Click on ADD.
  • Select Apache Tomcat v7.0.
  • Click Next
  • Give your Tomcat location in the Tomcat installation directory box.
Set Tomcat Runtime in eclipse

Creating a web service from a plain java class in eclipse

Let's develope a simple web application to demonstrate Java SOAP Webservices using Axis 2 and Tomcat. This Sample Application is all about querying information from a Web Application. For this, let us assume that we have a Employee Database maintained in the backend and we publish the various services to the Client in the WSDL file. Both types of clients, Browser Client and Console Client are included in the sample Application.

STEP 1 : Create a new Dynamic Web Project called TutorialsDesk.AxisWebService.
Java SOAP Webservice using Axis 2 and Tomcat Tutorial with examples

STEP 2 : Create a package com.tutorialsdesk.axis.bean under TutorialsDesk.AxisWebService.

STEP 3 : Create class Employee.java under package com.tutorialsdesk.axis.bean as given below

package com.tutorialsdesk.axis.bean;

import java.io.Serializable;

public class Employee implements Serializable {
 private static final long serialVersionUID = -1129402159048345204L; 
 String Name;
 String Department;
 int Age;
 double Salary;
 
 public Employee() {
  
 }
 
 public Employee(String name, String department, int age, double salary) {
  super();
  Name = name;
  Department = department;
  Age = age;
  Salary = salary;
 }
 
 public String getName() {
  return Name;
 }
 
 public void setName(String name) {
  Name = name;
 }
 
 public String getDepartment() {
  return Department;
 }
 
 public void setDepartment(String department) {
  Department = department;
 }
 
 public int getAge() {
  return Age;
 }
 
 public void setAge(int age) {
  Age = age;
 }
 
 public double getSalary() {
  return Salary;
 }
 
 public void setSalary(double salary) {
  Salary = salary;
 }

}



STEP 4 : Create a package com.tutorialsdesk.axis.util under TutorialsDesk.AxisWebService.

STEP 5 : Create class EmployeeDatabase.java under package com.tutorialsdesk.axis.util as given below

package com.tutorialsdesk.axis.util;

import java.util.*;
import com.tutorialsdesk.axis.bean.Employee;

public class EmployeeDatabase {

    private static List<employee> employees;
    public static List<employee> list(){
        return employees;
    }

    public static Employee getEmployee(String name){
        Iterator<employee> iterator = employees.iterator();
        while (iterator.hasNext()){
         Employee employee = (Employee) iterator.next();
            if (employee.getName().equals(name)){
                return employee;
            }
        }
        return null;
    }

    public static Employee getEmployeeData(String name){
        Iterator<employee> iterator = employees.iterator();
        while (iterator.hasNext()){
         Employee employee = (Employee) iterator.next();
            if (employee.getName().equals(name)){
                return employee;
            }
        }
        return null;
    }
    static {
     initEmployees();
    }

    static void initEmployees(){
     employees = new ArrayList<employee>();
     employees.add(new Employee("Rahul", "HR", 25, 15000.00));
     employees.add(new Employee("Zuzana", "Sales", 32, 48000.00));
     employees.add(new Employee("Martin", "Engineering",22, 32000.00));
     employees.add(new Employee("Sachin", "Engineering",25, 65000.00));
     employees.add(new Employee("Ondrej", "Operations",26, 25000.00));
    }
}


STEP 6 : Create a package com.tutorialsdesk.axis.services under TutorialsDesk.AxisWebService.

STEP 7 : Create class EmployeeService.java under package com.tutorialsdesk.axis.services as given below

package com.tutorialsdesk.axis.services;

import java.util.List;
import com.tutorialsdesk.axis.util.EmployeeDatabase;
import com.tutorialsdesk.axis.bean.Employee;

public class EmployeeService {

    public int getAgeForEmployee(String name){
        return EmployeeDatabase.getEmployee(name).getAge();
    }

    public String getDepartmentForEmployee(String name){
        return EmployeeDatabase.getEmployee(name).getDepartment();
    }

    public double getSalaryForEmployee(String name){
        return EmployeeDatabase.getEmployee(name).getSalary();
    }
    
    public Employee getEmployeeData(String name){

     return EmployeeDatabase.getEmployeeData(name);
    }
    
    public String getAllEmployees(){
        List<employee%gt; Employees = EmployeeDatabase.list();
        StringBuilder result = new StringBuilder();

        for(Employee employee : Employees){
            result.append(employee.getName() + " ");
        }
        return result.toString();
    }

}

STEP 8 : In Project folder right click on EmployeeService.java

STEP 9 : Click on Web Services - > Create Web Service.
Java SOAP Webservice using Axis 2 and Tomcat Tutorial with examples

STEP 10 : Select options publish the web service and Monitor the web service.
Create Web Service

STEP 11 : Click on Finish.

STEP 12 : It may take some time to finish all processes and you should see new project “TutorialsDesk.AxisWebServiceClient” created. Here is a final project structure.
Java SOAP Webservice using Axis 2 and Tomcat Tutorial with examples

STEP 13 : “TutorialsDesk.AxisWebService” and “TutorialsDesk.AxisWebServiceClient” both projects should be automatically deployed to server.
Tomcat Deployment

Invoking the Web Services

Use the following URL’s to access the various web services operations

  • http://localhost:8080/TutorialsDesk.AxisWebService/services/EmployeeService?method=getAllEmployees
  • http://localhost:8080/TutorialsDesk.AxisWebService/services/EmployeeService?method=getAgeForEmployee&name=Martin
  • http://localhost:8080/TutorialsDesk.AxisWebService/services/EmployeeService?method=getSalaryForEmployee&name=Ondrej
  • http://localhost:8080/TutorialsDesk.AxisWebService/services/EmployeeService?method=getDepartmentForEmployee&name=Rahul
  • http://localhost:8080/TutorialsDesk.AxisWebService/services/EmployeeService?method=getEmployeeData&name=Zuzana

Creating a Console-based Client

Let us see how to access the Web Service using the Axis Client API with a normal Console Client. Following is the code snippet for the same.
package com.tutorialsdesk.axis.client;

import java.net.URL;

import org.apache.axis.client.Service;
import org.apache.axis.client.Call;
public class EmployeeServiceClient {

    public static void main(String[] args) {

        try{
            URL url = new URL("http://localhost:8080/TutorialsDesk.AxisWebService/services/EmployeeService");

            Service service = new Service();

            Call call  = (Call)service.createCall();
            call.setTargetEndpointAddress(url);
            Object result = call.invoke("getAllEmployees", new Object[]{});
            System.out.println(result);

            result = call.invoke("getAgeForEmployee", new Object[]{"Martin"});
            System.out.println(result);

            result = call.invoke("getSalaryForEmployee", new Object[]{"Ondrej"});
            System.out.println(result);

            result = call.invoke("getDepartmentForEmployee", new Object[]{"Rahul"});
            System.out.println(result);

        }catch(Exception exception){
            exception.printStackTrace();
        }
    }
}


Hope we are able to explain you Java SOAP Webservices using Axis 2 and Tomcat , if you have any questions or suggestions please write to us using contact us form.(Second Menu from top left).

Please share us on social media if you like the tutorial.
SHARE
    Blogger Comment
    Facebook Comment