Simple Application to use SOAP webservice

Technical Articles

Today we are going to implement the practical example of SOAP webservices and consume the services to transport data over the network.

You need to follow some simple steps in order to create a soap web service. Here we are creating a service to add person, get person and delete person using web service. First of all let’s create a Dynamic Web Project in Eclipse. after that you need to create a class that will be having variables id ,name and age for the person. After that you can generate getters ,setters and a toString method to print the details on Client side.

package com.soap.example;

public class Person implements Serializable{

	private static final long serialVersionUID = 1L;

	private int id;
	private String name;
	private int age;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";
	}
}

We are implementing Serializable interface in the above class because we need to store the value and transport it over the network.

Next, we will create an interface with the simple methods to add, delete and get the person object.

package com.soap.example.service;

import com.soap.example.Person;

public interface PersonService {

	public boolean addPerson(Person p);
	public boolean deletePerson(int id);
	public Person getPerson(int id);
}

After that we will be implementing the PersonService interface and provide the implementation for the methods. So for now our business logic is done then lets create a web service to consume these methods.

package com.soap.example.service;

import java.util.HashMap;
import java.util.Map;

import com.soap.example.Person;

public class PersonServiceImpl implements PersonService {

	private static Map<Integer,Person> persons = new HashMap<Integer,Person>();
	@Override
	public boolean addPerson(Person p) {
		if(persons.get(p.getId())!=null) return false;
		persons.put(p.getId(),p);
		return true;
	}

	@Override
	public boolean deletePerson(int id) {
		if(persons.get(id)==null) return false;
		persons.remove(id);
		return true;
	}

	@Override
	public Person getPerson(int id) {
		return persons.get(id);
	}
}

To create a Web Service just go to File->New->Other and search for web service

After that you can select Web Service Type whether is Bottom up or Top down. In top down you start with WSDL and then create all necessary Java methods from WSDL where in bottom up you start with the Java method and generate the WSDL from it. After that select the full path of your Service implementation class

After that click next ,you will be asked to start the server if it is not started and then you will get the option to launch that will open the webpage where you can see all your implemented methods.

where you can test your implemented methods and also you can see the wsdl file using the endpoint. Endpoint?wsdl . In this case endpoint is:-

http://localhost:8081/SoapWebService/services/PersonServiceImpl

Let me know if you have any problem with creating it or you can share your experience using it.

Leave a Reply

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