March 01, 2016

Java SOAP Web Service Example..

Here we will learn how we can create a SOAP web service and it’s client program using using Apache Axis that is integrated in the Eclipse and provide quick and easy way to transform a application into Java Web Service and create client stubs with test JSP page for testing purpose.

First of all we will create a simple Dynamic Web Project in Eclipse that will contain the business logic for our application.

Click on Next button above and you will get next page to provide your web project name and Target Runtime. Here I am using Apache Tomcat 8.0, if you can use any other standard servlet container too.
Click on Next and you will be asked to provide “Context Root” and Content Directory location. You can leave them as default.

Click on Finish and Eclipse will create the project skeleton for you.

Now we will publish a web service that can be used to add/delete/get an object. For that first step is to create a model bean.

package com.soap.bean;
import java.io.Serializable;
public class Person implements Serializable{
    private static final long serialVersionUID = -7577579081118070434L;
    
    private String name;
    private int age;
    private int id;
   
    @Override
    public String toString(){
        return id+"|"+name+"|"+age;
    }
    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;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }  
}


Person bean is implementing Serializable interface because we will be transporting it over the network. Also, toString method is implemented that will be used when we will print this object at client side.

Now we will create service classes, so we will have an interface as PersonService and class PersonServiceImpl which implements it.

package com.soap.service;
import com.soap.bean.Person;

public interface PersonService {
    public boolean addPerson(Person p);   
    public boolean deletePerson(int id);    
    public Person getPerson(int id);    
    public Person[] getAllPersons();
}


package com.soap.service;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import com.soap.bean.Person;

public class PersonServiceImpl implements PersonService {

    private static Map persons = new HashMap();
    
    @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);
    }
    @Override
    public Person[] getAllPersons() {
        Set ids = persons.keySet();
        Person[] p = new Person[ids.size()];
        int i=0;
        for(Integer id : ids){
            p[i] = persons.get(id);
            i++;
        }
        return p;
    }
}


Our business logic is ready, next step is to use Eclipse to create a web service application from this. Create a new project and select Web Service wizard.
Click Next button and you will get a page where web service and it’s client details have to be provided. This is the most important page in creating web service. Make sure you select “Web Service type” as “Bottom up Java bean Web Service” because we are implementing with bottom up approach.

There are two ways to create web service:
1. Contract last or Bottom up approach: In this approach we first create the implementation and then generate the WSDL file from it. Our implementation fits in this category.
2. Contract first or Top Down Approach: In this approach, we first create the web service contract i.e. WSDL file and then create the implementation for it.
In the service implementation, select 'PersonServiceImpl'. Make sure you move the slider in service and client type so that it can generate client program and also UI to test our web service. Check for the configurations in web service implementation, you should provide correct details for Server runtime, Web service runtime and service project. Usually they are auto populated and you don’t need to make any changes here.

For client configurations, you can provide the client project name as you like. I have left it to default as SoapTestClient.

Click on Next button and then you will be able to choose the methods that you want to expose as web service. You will also be able to choose the web service style as either document or literal. You can change the WSDL document name but it’s good to have it with implementation class name to avoid confusion later on.
Click on Next button and you will get server startup page, click on the “Start server” button and then next button will enable.
Click on Next button and you will get a page to launch the “Web Services Explorer”.
Click on Launch button and it will open a new window in the browser where you can test your web service before moving ahead with the client application part.
We can do some sanity testing here.

Click on the Next button in the Eclipse web services popup window and you will get a page for source folder for client application.
Click on Next button and you will get different options to choose as test facility. Here we will go ahead with JAX-RPC JSPs so that client application will generate a JSP page that we can use.

The methods getEndpoint() and setEndpoint(String) are added so that we can use to get the web service endpoint URL and we can set it to some other URL in case we move our server to some other URL endpoint.

Click on Finish button and Eclipse will create the client project in your workspace.

Add Person:
Get Person:
Get All Person: If you invoke this, you will get a result as:
                          [com.soap.bean.Person@88f1a2b4]

The Person details are not printed in the results section, this is because it’s auto generated code and we need to refactor it a little to get the desired output.

Open Result.jsp in the client project and you will see it’s using switch case to generate the result output. search 'getAllPersons() method, it was case 31 in my case.

Change the code like this:

case 31:
        gotMethod = true;
        com.soap.bean.Person[] getAllPersons31mtemp = samplePersonServiceImplProxyid.getAllPersons();
if(getAllPersons31mtemp == null){
%>
<%=getAllPersons31mtemp %>
<%
}else{
        String tempreturnp32 = null;
        if(getAllPersons31mtemp != null){
             java.util.List listreturnp43= java.util.Arrays.asList(getAllPersons31mtemp);
             //tempreturnp43 = listreturnp43.toString();
             for(com.soap.bean.Person p : listreturnp43){
                 int id = p.getId();
                 int age = p.getAge();
                 String name=p.getName();
                 %>
                 <%=id%>::<%=name %>::<%=age %>
                 <%
                 }

             }
}

Save the jsp and from browser invoke getAllPersons service, it will show below result:

SOAP Web Service WSDL and Configs

If you have noticed, PersonServiceImpl.wsdl is generated under 'wsdl' folder of 'WebContent'. And web.xml is modified to use Apache Axis as front controller for web service.

Most of the code is automatically generated by Eclipse, all we have to do is to write business logic for our web service.

ALSO READ: Web Services..

-K Himaanshu Shuklaa..

No comments:

Post a Comment

RSSChomp Blog Directory