RESTful Web Services in Java

Read about REST here…
In the example program REST is implemented using Jersey Framework.
Download Jersey framework from here…
I used Dynamic Web Project in Eclipse for coding. Extract jersey zip file and copy all the .jar files from api, ext, lib folders to WebContent/WEB-INF/lib folder.

Employee.java

     
package in.theinsanetechie.rest;

import java.io.Serializable;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "employee")
public class Employee implements Serializable {

    private static final long serialVersionUID = 1L;
    private int empcode;
    private String name;
    private String designation;
    private float basicpay;

    public Employee() {
    }

    public Employee(int empcode, String name, String designation, float basicpay) {
        super();
        this.empcode = empcode;
        this.name = name;
        this.designation = designation;
        this.basicpay = basicpay;
    }

    public int getEmpcode() {
        return empcode;
    }

    @XmlElement
    public void setEmpcode(int empcode) {
        this.empcode = empcode;
    }

    public String getName() {
        return name;
    }

    @XmlElement
    public void setName(String name) {
        this.name = name;
    }

    public String getDesignation() {
        return designation;
    }

    @XmlElement
    public void setDesignation(String designation) {
        this.designation = designation;
    }

    public float getBasicpay() {
        return basicpay;
    }

    @XmlElement
    public void setBasicpay(float basicpay) {
        this.basicpay = basicpay;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Employee other = (Employee) obj;
        if (Float.floatToIntBits(basicpay) != Float.floatToIntBits(other.basicpay))
            return false;
        if (designation == null) {
            if (other.designation != null)
                return false;
        } else if (!designation.equals(other.designation))
            return false;
        if (empcode != other.empcode)
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }

}

EmployeeAccessObject.java

     
package in.theinsanetechie.rest;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

public class EmployeeAccessObject {

    private String fileName = "Employee.dat";

    @SuppressWarnings("unchecked")
    public List getFullList() {
        List list = null;
        File file = new File(fileName);
        try {
            if (file.exists()) {
                FileInputStream fileInputStream = new FileInputStream(file);
                ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
                list = (List) objectInputStream.readObject();
                objectInputStream.close();
            } else {
                Employee employee = new Employee(1001, "The Insane Techie", "Dev", 99999.99f);
                list = new ArrayList();
                list.add(employee);
                saveList(list);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        return list;
    }

    public Employee get(int empcode) {
        List list = getFullList();

        for (Employee temp : list) {
            if (temp.getEmpcode() == empcode) {
                return temp;
            }
        }

        return null;
    }

    public boolean addToList(Employee emp) {
        List list = getFullList();
        boolean exists = false;
        for (Employee temp : list) {
            if (temp.getEmpcode() == emp.getEmpcode()) {
                exists = true;
                break;
            }
        }
        if (!exists) {
            list.add(emp);
            saveList(list);
            return true;
        }
        return false;
    }

    public boolean updateList(Employee emp) {
        List list = getFullList();
        for (Employee temp : list) {
            if (temp.getEmpcode() == emp.getEmpcode()) {
                int index = list.indexOf(temp);
                list.set(index, emp);
                saveList(list);
                return true;
            }
        }
        return false;
    }

    public boolean deleteFromList(int empcode) {
        List list = getFullList();

        for (Employee temp : list) {
            if (temp.getEmpcode() == empcode) {
                int index = list.indexOf(temp);
                list.remove(index);
                saveList(list);
                return true;
            }
        }
        return false;
    }

    private void saveList(List list) {

        File file = new File(fileName);
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
            objectOutputStream.writeObject(list);
            objectOutputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

EmployeeService.java

package in.theinsanetechie.rest;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.OPTIONS;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

@Path("/EmployeeService")
public class EmployeeService {

    EmployeeAccessObject accessObject = new EmployeeAccessObject();
    private static final String SUCCESS = "success";
    private static final String FAILURE = "failure";

    @GET
    @Path("/employees")
    @Produces(MediaType.APPLICATION_XML)
    public List getFullList() {
        return accessObject.getFullList();
    }

    @GET
    @Path("/employees/{empcode}")
    @Produces(MediaType.APPLICATION_XML)
    public Employee get(@PathParam("empcode") int empcode) {
        return accessObject.get(empcode);
    }

    @PUT
    @Path("/employees")
    @Produces(MediaType.APPLICATION_XML)
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public String add(@FormParam("empcode") int empcode, @FormParam("name") String name,
            @FormParam("designation") String designation, @FormParam("basicpay") float basicpay,
            @Context HttpServletResponse servletResponse) throws IOException {
        Employee emp = new Employee(empcode, name, designation, basicpay);
        boolean result = accessObject.addToList(emp);
        if (result == true) {
            return SUCCESS;
        }
        return FAILURE;
    }

    @POST
    @Path("/employees")
    @Produces(MediaType.APPLICATION_XML)
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public String update(@FormParam("empcode") int empcode, @FormParam("name") String name,
            @FormParam("designation") String designation, @FormParam("basicpay") float basicpay,
            @Context HttpServletResponse servletResponse) throws IOException {
        Employee emp = new Employee(empcode, name, designation, basicpay);
        boolean result = accessObject.updateList(emp);
        if (result == true) {
            return SUCCESS;
        }
        return FAILURE;
    }

    @DELETE
    @Path("/employees/{empcode}")
    @Produces(MediaType.APPLICATION_XML)
    public String deleteUser(@PathParam("empcode") int empcode) {
        boolean result = accessObject.deleteFromList(empcode);
        if (result == true) {
            return SUCCESS;
        }
        return FAILURE;
    }

    @OPTIONS
    @Path("/employees")
    @Produces(MediaType.APPLICATION_XML)
    public String getSupportedOperations() {
        return "GET, PUT, POST, DELETE";
    }
}
     

WebServiceTester.java

     
package in.theinsanetechie.rest;

import java.util.List;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;

public class WebServiceTester {

    private Client client;
    private String REST_SERVICE_URL = "http://localhost:8080/EmployeeManagement/rest/EmployeeService/employees";
    private static final String SUCCESS = "success";
    private static final String PASS = "pass";
    private static final String FAIL = "fail";

    private void init() {
        this.client = ClientBuilder.newClient();
    }

    public static void main(String[] args) {

        WebServiceTester tester = new WebServiceTester();
        tester.init();
        tester.testGetFullList();
        tester.testGet();
        tester.testUpdate();
        tester.testAdd();
        tester.testDelete();
    }

    private void testGetFullList(){
      GenericType> list = new GenericType>() {};
      List employees = client
        .target(REST_SERVICE_URL)
        .request(MediaType.APPLICATION_XML)
        .get(list);
      String result = PASS;
      if(employees.isEmpty()){
       result = FAIL;
      }
      System.out.println("Test case name: testGetFullList, Result: " + result );
     }

    private void testGet() {
        Employee sampleEmp = new Employee();
        sampleEmp.setEmpcode(1001);

        Employee emp = client.target(REST_SERVICE_URL).path("/{empcode}").resolveTemplate("empcode", 1001)
                .request(MediaType.APPLICATION_XML).get(Employee.class);
        String result = FAIL;
        if (sampleEmp != null && sampleEmp.getEmpcode() == emp.getEmpcode()) {
            result = PASS;
        }
        System.out.println("Test case name: testGet, Result: " + result);
    }

    private void testUpdate() {
        Form form = new Form();
        form.param("empcode", "1001");
        form.param("name", "Dhanoop Bhaskar");
        form.param("designation", "Computer Scientist");
        form.param("basicpay", "100000.00f");

        String callResult = client.target(REST_SERVICE_URL).request(MediaType.APPLICATION_XML)
                .post(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE), String.class);
        String result = PASS;
        if (!SUCCESS.equals(callResult)) {
            result = FAIL;
        }

        System.out.println("Test case name: testUpdate, Result: " + result);
    }

    private void testAdd() {
        Form form = new Form();
        form.param("empcode", "1002");
        form.param("name", "The Insane Techie");
        form.param("designation", "Computer Scientist");
        form.param("basicpay", "100000.00f");

        String callResult = client.target(REST_SERVICE_URL).request(MediaType.APPLICATION_XML)
                .put(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE), String.class);

        String result = PASS;
        if (!SUCCESS.equals(callResult)) {
            result = FAIL;
        }

        System.out.println("Test case name: testAdd, Result: " + result);
    }

    private void testDelete() {
        String callResult = client.target(REST_SERVICE_URL).path("/{empcode}").resolveTemplate("empcode", 1002)
                .request(MediaType.APPLICATION_XML).delete(String.class);

        String result = PASS;
        if (!SUCCESS.equals(callResult)) {
            result = FAIL;
        }

        System.out.println("Test case name: testDelete, Result: " + result);
    }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>Employee Management</display-name>
    <servlet>
        <servlet-name>Jersey RESTful Application</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
            <init-param>
                <param-name>jersey.config.server.provider.packages</param-name>
                <param-value>in.theinsanetechie.rest</param-value>
            </init-param>
        </servlet>
    <servlet-mapping>
    <servlet-name>Jersey RESTful Application</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>