Saturday, 3 August 2019

Spring boot: Adding Rest APIs


This is continuation to my previous post. In this post, I am going to explain how to add rest end points to the previous application.

You can download the previous working application from this link.

In this post, I am going to add below rest apis to the application.
API
Method
Description
/employees
GET
Get all the employees
/employees
POST
Create new employee
/employee/{id}
GET
Get employee by id
/employee/{id}
DELETE
Delete employee by id
/employee/{id}
PUT
Update employee

Previous project structure looks like below.


Step 1: Create a package ‘com.sample.app.model’ and define Employee class like below.

Employee.java
package com.sample.app.model;

public class Employee {
    private int id;
    private String firstName;
    private String lastName;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

}

Step 2: Create a package ‘com.sample.app.util’ and define EmployeeUtil class like below.

EmployeeUtil.java
package com.sample.app.util;

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

import com.sample.app.model.Employee;

public class EmployeeUtil {

    private static int idCounter = 1;

    private static final Map<Integer, Employee> employeesRepo = new HashMap<>();

    private static Employee buildEmployee(String firstName, String lastName) {
        Employee emp = new Employee();
        emp.setId(idCounter);
        emp.setFirstName(firstName);
        emp.setLastName(lastName);

        idCounter++;

        return emp;
    }

    static {
        Employee emp1 = buildEmployee("Deepak", "Moud");
        Employee emp2 = buildEmployee("Srinivasa Rao", "Gumma");
        Employee emp3 = buildEmployee("Purna Chandra", "Rao");
        Employee emp4 = buildEmployee("Madhavi Latha", "Gumma");
        Employee emp5 = buildEmployee("Raghava", "Reddy");

        employeesRepo.put(emp1.getId(), emp1);
        employeesRepo.put(emp2.getId(), emp2);
        employeesRepo.put(emp3.getId(), emp3);
        employeesRepo.put(emp4.getId(), emp4);
        employeesRepo.put(emp5.getId(), emp5);

    }

    public static Collection<Employee> all() {
        return employeesRepo.values();
    }

    public static Employee byId(int id) {
        return employeesRepo.get(id);
    }

    public static Employee create(String firstName, String lastName) {
        Employee emp = buildEmployee(firstName, lastName);
        employeesRepo.put(emp.getId(), emp);
        return emp;
    }

    public static Employee create(Employee emp) {
        return create(emp.getFirstName(), emp.getLastName());
    }

    public static Employee delete(int id) {
        return employeesRepo.remove(id);
    }

    public static Employee updateById(int id, Employee emp) {
        emp.setId(id);
        return employeesRepo.put(id, emp);
    }
}

Step 3: Create EmployeeController class in ‘com.sample.app.controller’ package.


EmployeeController.java
package com.sample.app.controller;

import java.util.Collection;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.sample.app.model.Employee;
import com.sample.app.util.EmployeeUtil;

@RestController
@RequestMapping("api/v1/")
public class EmployeeController {

    @RequestMapping(value = "employees", method = RequestMethod.GET)
    public Collection<Employee> all() {
        return EmployeeUtil.all();
    }

    @RequestMapping(value = "employees", method = RequestMethod.POST)
    public Employee create(@RequestBody Employee emp) {
        return EmployeeUtil.create(emp);
    }

    @RequestMapping(value = "employees/{id}", method = RequestMethod.GET)
    public Employee byId(@PathVariable int id) {
        return EmployeeUtil.byId(id);
    }

    @RequestMapping(value = "employees/{id}", method = RequestMethod.DELETE)
    public Employee deleteById(@PathVariable int id) {
        return EmployeeUtil.delete(id);
    }

    @RequestMapping(value = "employees/{id}", method = RequestMethod.PUT)
    public Employee updateById(@PathVariable int id, @RequestBody Employee emp) {
        return EmployeeUtil.updateById(id, emp);
    }

}

@RequestMapping("api/v1/")
This is class level request mapping. For example, I defined ‘all’ method like below. so the uri "api/v1/employees" is mapped to all() method

@RequestMapping(value = "employees", method = RequestMethod.GET)
public Collection<Employee> all() {
    return EmployeeUtil.all();
}

RequestMethod.GET : represents HTTP GET method
RequestMethod.GET : represents HTTP GET method
RequestMethod.GET : represents HTTP GET method
RequestMethod.GET : represents HTTP GET method
RequestMethod.GET : represents HTTP GET method

@RequestBody Employee emp
Maps request payload to employee object.


App.java
package com.sample.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}


Run App.java and hit the url 'http://localhost:8080/api/v1/employees', you can see all the employee’s information.

How to get employee with id 4?
Method: GET

How to create new employee?
Method: POST
Body:
{
    "firstName" : "Joel",
    "lastName" : "Chelli"
}

How to update an employee?
Method: PUT
Body:
{
         "firstName" : "Rahim",
         "lastName" : "Akbar"
}

How to delete an employee?
Method: DELETE


Total project structure looks like below.

You can download the complete working application from this link.


Previous                                                    Next                                                    Home

No comments:

Post a Comment