Skip to main content

RESTful Web service Implementation

RESTful Web service implementation



IF you are reading this post you should probably understood what REST services are. Before building a REST service you should understand 5 methods of REST services.
  1. GET - used to retrieve resource from web services
  2. POST - used to update an existing resource or create a new resource.
  3. PUT - used to create new resources in web services
  4. DELETE - used to delete resources in web service
  5. OPTIONS - used to get the supported operations on a resource
Now lets begin the REST web service implementation.

I'll be using IntelliJ idea for development. and Jersey frame to create RESTful web services. Jersey framework implements JAX-RS 2.0 API, which is a standard specification to create RESTful Web Services.

and download javaee API from here https://mvnrepository.com/artifact/javax/javaee-api/7.0

Before starting the project download and install glassfish server.

First create a new project from IntelliJ

Then select Web Application.

Then scroll below and select RESTful Web services and tick download from below dialog.

Then enter project name as UserServiecs and click finish.

IntelliJ automatically downloads jersey jar files and save it lib folder in your project file.

Setting up GlassFish server

Go to settings > tools > External engines select glass fish installed directory. click ok.

Creating the source files

Now let us create actual source file for this project. Right click src file and create new Java class enter models.Employee

IntelliJ will create models package and Employee class . I created the package to differentiate it from other class so you can easily understand.

Employee.java
package models;
/** * Created by ShimaK on 12-Apr-17. */public class Employee {
    private String name;
    private String position;
    private String username;
    private String password;
    public Employee(String name, String position, String username, String password) {
        this.name = name;
        this.position = position;
        this.username = username;
        this.password = password;
    }

    public String getName() {
        return name;
    }

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

    public String getPosition() {
        return position;
    }

    public void setPosition(String position) {
        this.position = position;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override    
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", position='" + position + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

Create an Application class

create new Java class and type service.Appication

package service;
import javax.ws.rs.ApplicationPath;
/** * Created by ShimaK on 12-Apr-17. */
@ApplicationPath("/api")
public class Application extends javax.ws.rs.core.Application{

}

Create an EmployeeController class

create new Java class and type controller.EmployeeController
and save the following code

package controllers;
import models.Employee;
/** * Created by ShimaK on 12-Apr-17. */public class EmployeeController {
    public Employee getEmployee() {
        return new Employee("Shimak", "Student", "shimak2", "123");    }
}

getEmployee method will create an instance of employee

Create an EmployeeService class

create an EmployeeService class in controller package and add the following code.

package service;
import controllers.EmployeeController;
import models.Employee;
import javax.json.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
/** * Created by ShimaK on 12-Apr-17. */
@Path("/employee")
public class EmployeeService {
    @GET
    @Produces({MediaType.APPLICATION_JSON})
    public JsonObject getEmployee() {
        Employee employee = new EmployeeController().getEmployee();
        JsonObjectBuilder objbuild = Json.createObjectBuilder();
        objbuild.add("name", employee.getName());
        objbuild.add("position", employee.getPosition());
        objbuild.add("username", employee.getName());
        objbuild.add("password", employee.getPassword());
        return objbuild.build();
    }
}

Okay we are almost complete. now we have to test from client side. intelliJ has plugin Test RestFul web services to get that go to tools > Test RESTful Web service


add following http address to Host/port box http://localhost:8080
then add UserServices_war_exploded/api/employee link to path then run

you will get the response like this



Thank you for reading .

Comments