This tutorial is purely on programming SOAP Web Services with JAVA. First of I will give a sample case study. Then I will implement the same. By reading this article, You can learn the implementation of SOAP web services with JAVA. You need to have Tomcat server configured Eclipse.

What is SOAP : SOAP stands for Simple Object Access Protocol. We can send messages by using this Protocol. SOAP is based on XML, so it is independent of programming platform.

SOAP Message Syntax : <? xml version = "1.0" ?> < soap : Envelope xmlns:soap= "http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle= "http://www.w3.org/2001/12/soap-encoding" > < soap : Header > ... </ soap : Header > < soap : Body > ... < soap : Fault > ... </ soap : Fault > </ soap : Body > </ soap : Envelope >

Case Study :

This project is for maintaining Employee information. We have to convert below Case Study into Web Service project.

Create Employee Class Create EmployeeService Class with methods createEmployee(String id, String name, String email), deleteEmployee(String id), selectEmployees() Create one global HashMap to maintain Employee Info. createEmployee is for creating employees, This method takes Employee object as input and returns String deleteEmployee is for deleting employees, This method takes Employee ID (String) as input and returns String selectEmployees is for reading all employee info. This method returns List of Employee details Annotations :

@WebService Annotation

@WebService( portName = "EmployeePort" , serviceName = "EmployeeService" , targetNamespace = "http://blog.sodhanalibrary.com/wsdl" , endpointInterface = "com.services.EmplyeeInterface" )

This annotation is for java class which works as Sevice

portName - specifies wsdl:port

serviceName - specifies Service Name

targetNamespace - specifies Namespace for WSDL

endpointInterface - java interface class which Service class implements

@WebMethod Annotation

This annotation is for method in service class which implements functionality

action - specifies SOAP action

operationName - specifies soap:operation

exclude - exclude the method from Web Service

@WebMethod(action = "create" , operationName = "createEmployee" , exclude = false )

@WebParam Annotation This annotation is for defining request parameters. @WebParam(name = "i" ) int i

Setup Apache Axis2

Apache Axis 2 is Web Service engine which supports SOAP messages.

Axis2 Preferences

Download the Axis 2 binary distribution (here) Extract it Enter Eclipse and go to Window > Preferences > Web Services > Axis2 Preferences In the Axis2 runtime location field, points to your Axis2 installation directory (the one you created in step 2) Click Apply Close Preferences and try again

Server and Runtime

Enter Eclipse and go to Window > Preferences > Web Services > Server and Runtime In the Web Service runtime field, points to your Axis2 installation directory (the one you created in step 2) Click Apply Close Preferences and try again

Setup Chrome / Firefox SOAP Web Service Testing Plugin

Wizdler in chrome. You can find this plugin Installin chrome. You can find this plugin here

or

SOA Client for Firefox. You can find this plugin Installfor Firefox. You can find this plugin here

Implementation

Create New Dynamic Web Project SimpleSoapExample in Eclipse

Create com.services java package

Create Employee Pojo Class

package com.services ; public class Employee { String id; String name; String email; public String getId () { return id; } public void setId ( String id ) { this . id = id; } public String getName () { return name; } public void setName ( String name ) { this . name = name; } public String getEmail () { return email; } public void setEmail ( String email ) { this . email = email; } }



Here I have used Employee Array because JAX-RPC doesn't support any collections. This class is for showing list of Employees

package com.services ; public class EmployeeList { private Employee [] employeeList; private int count; public Employee [] getEmployeeList () { return employeeList; } public void setEmployeeList ( Employee [] employeeList ) { this . employeeList = employeeList; } public int getCount () { return count; } public void setCount ( int count ) { this . count = count; } } Create below EmployeeService class in the com.services package

This is Service class. We can access methods in this class through SOAP web service Create EmployeeList Pojo class like belowHere I have used Employee Array because JAX-RPC doesn't support any collections. This class is for showing list of EmployeesCreate belowclass in thepackageThis is Service class. We can access methods in this class through SOAP web service

SOAP Body contains messageSOAP Header contains data other than message (Authentication info, Login User ID...etc)SOAP Fault contains reason for failure.