Representational state transfer (REST) is a software architectural style that defines a set of constraints to be used for creating Web services. Web services that conform to the REST architectural style, called RESTful Web services, provide interoperability between computer systems on the Internet. RESTful Web services allow the requesting systems to access and manipulate textual representations of Web resources by using a uniform and predefined set of stateless operations. Other kinds of Web services, such as SOAP Web services, expose their own arbitrary sets of operations.
In this article, we will understand how to create a rest API using spring boot.
Spring is widely used for creating scalable applications. For web applications, Spring provides Spring MVC which is a widely used module of spring which is used to create scalable web applications. But the main disadvantage of spring projects is that configuration is really time-consuming and can be a bit overwhelming for the new developers. The solution to this is Spring Boot. Spring Boot is built on the top of the spring and contains all the features of spring. In this article, we will create a REST API to add employees to the employee list and get the list of employees. In order to do this, we first have to create a simple Spring Boot project in any of the IDE’s and follow the steps:
- Initially, we need to define the employee entity. Therefore, the following employee class is defined:
packagecom.example.demo;ÂÂ// Creating an entity EmployeepublicclassEmployee {   ÂpublicEmployee() {}   Â// Parameterized Constructor   Â// to assign the values   Â// to the properties of   Â// the entity   ÂÂpublicEmployee(       ÂInteger id, String firstName,       ÂString lastName, String email)   Â{       Âsuper();       Âthis.id = id;       Âthis.firstName = firstName;       Âthis.lastName = lastName;       Âthis.email = email;       Â Â   Â}   Â ÂprivateInteger id;   Â ÂprivateString firstName;   Â ÂprivateString lastName;   Â ÂprivateString email;   Â// Overriding the toString method   Â// to find all the values   Â@Override ÂpublicString toString()   Â{       Âreturn"Employee [id="           Â+ id +", firstName="           Â+ firstName +", lastName="           Â+ lastName +", email="           Â+ email +"]";       Â Â   Â}   Â// Getters and setters of   Â// the properties   ÂpublicInteger getId()   Â{       ÂÂreturnid;   Â}   ÂpublicvoidsetId(Integer id)   Â{       ÂÂthis.id = id;   Â}   ÂpublicString getFirstName()   Â{       ÂÂreturnfirstName;   Â}   ÂpublicvoidsetFirstName(       ÂString firstName)   Â{       ÂÂthis.firstName = firstName;   Â}   ÂpublicString getLastName()   Â{       ÂÂreturnlastName;   Â}   ÂpublicvoidsetLastName(       ÂString lastName)   Â{       ÂÂthis.lastName = lastName;   Â}   ÂpublicString getEmail()   Â{       ÂÂreturnemail;   Â}   ÂpublicvoidsetEmail(String email)   Â{       ÂÂthis.email = email;   Â}} - Now, we need to create a storage class which stores the list of all the employees:
packagecom.example.demo;ÂÂimportjava.util.ArrayList;importjava.util.List;ÂÂ// Class to store the list of// all the employees in an// Array ListpublicclassEmployees {   ÂprivateList<Employee> employeeList;   Â// Method to return the list   Â// of employees   ÂpublicList<Employee> getEmployeeList()   Â{       Âif(employeeList ==null) {           ÂemployeeList               Â=newArrayList<>();           Â   Â       Â}       ÂreturnemployeeList;       Â Â   Â}   Âpublicvoid   ÂsetEmployeeList(       ÂList<Employee> employeeList)   Â{       Âthis.employeeList           Â= employeeList;   Â}} - Till now, we have defined the entity employee and created a storage class. Now, we need to access the employees. So, we create a class from where we will create an object of the storage class to store the employees:
packagecom.example.demo;ÂÂimportorg.springframework   Â.stereotype   Â.Repository;ÂÂ// Importing the employees class to// use the defined properties// in this classimportcom.example.demo.Employees;ÂÂ@RepositoryÂÂ// Class to create a list// of employeespublicclassEmployeeDAO {   ÂprivatestaticEmployees list       Â=newEmployees();   Â// This static block is executed   Â// before executing the main   Â// block   Âstatic   Â{       Â// Creating a few employees       Â// and adding them to the list       Âlist.getEmployeeList().add(           ÂnewEmployee(               Â1,               Â"Prem",               Â"Tiwari",               Â"chapradreams@gmail.com"));       Âlist.getEmployeeList().add(           ÂnewEmployee(               Â2,"Vikash",               Â"Kumar",               Â"abc@gmail.com"));       Âlist.getEmployeeList().add(           ÂnewEmployee(               Â3,"Ritesh",               Â"Ojha",               Â"asdjf@gmail.com"));       Â Â   Â}   Â// Method to return the list   ÂpublicEmployees getAllEmployees()   Â{       Âreturnlist;   Â}   Â Â       Â// Method to add an employee       Â// to the employees list       Âpublicvoid       ÂaddEmployee(Employee employee)   Â{       Âlist.getEmployeeList()           Â.add(employee);       Â Â   Â}} - Finally, we need to create a controller class which is the actual implementation of the REST API. According to the REST rules, every new entry in the database has to be called by the POST method and all the requests from the database must be called using the GET method. The same methods are implemented in the following code:
packagecom.example.demo;ÂÂimportjava.net.URI;importorg.springframework.beans   Â.factory.annotation.Autowired;importorg.springframework.http   Â.ResponseEntity;importorg.springframework.web.bind   Â.annotation.GetMapping;importorg.springframework.web.bind   Â.annotation.PostMapping;importorg.springframework.web.bind   Â.annotation.RequestBody;importorg.springframework.web.bind   Â.annotation.RequestMapping;importorg.springframework.web.bind   Â.annotation.RestController;importorg.springframework.web.servlet   Â.support.ServletUriComponentsBuilder;ÂÂ// Import the above-defined classes// to use the properties of those// classesimportcom.example.demo.Employees;importcom.example.demo.EmployeeDAO;importcom.example.demo.Employee;ÂÂ// Creating the REST controller@RestController@RequestMapping(path ="/employees")publicclassEmployeeController {   Â@Autowired ÂprivateEmployeeDAO employeeDao;   Â Â       Â// Implementing a GET method       Â// to get the list of all       Â// the employees Â@GetMapping(       Âpath ="/",       Âproduces ="application/json")   ÂpublicEmployees getEmployees()   Â{       ÂreturnemployeeDao           Â.getAllEmployees();   Â}   Â Â       Â// Create a POST method       Â// to add an employee       Â// to the list Â@PostMapping(       Âpath ="/",       Âconsumes ="application/json",       Âproduces ="application/json")   ÂpublicResponseEntity<Object> addEmployee(       Â@RequestBodyEmployee employee)   Â{       Â// Creating an ID of an employee       Â// from the number of employees       ÂInteger id           Â= employeeDao                 Â.getAllEmployees()                 Â.getEmployeeList()                 Â.size()             Â+1;       Âemployee.setId(id);       ÂemployeeDao           Â.addEmployee(employee);       ÂURI location           Â= ServletUriComponentsBuilder                 Â.fromCurrentRequest()                 Â.path("/{id}")                 Â.buildAndExpand(                     Âemployee.getId())                 Â.toUri();       Â   ÂreturnResponseEntity           Â.created(location)           Â.build();   Â}} - After implementing all the classes in the project, run the project as Spring Boot App. Once the server starts running, we can send the requests through the browser or postman. We can access the running app by going into the following URL:
http://localhost:8080/employees/
Output: The following is the output generated on running the above project:

