How to create a REST API using Java Spring Boot
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:
package com.example.demo;
// Creating an entity Employee
public class Employee {
public Employee() {}
// Parameterized Constructor
// to assign the values
// to the properties of
// the entity
public Employee(
Integer id, String firstName,
String lastName, String email)
{
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
private Integer id;
private String firstName;
private String lastName;
private String email;
// Overriding the toString method
// to find all the values
@Override
public String toString()
{
return "Employee [id="
+ id + ", firstName="
+ firstName + ", lastName="
+ lastName + ", email="
+ email + "]";
}
// Getters and setters of
// the properties
public Integer getId()
{
return id;
}
public void setId(Integer 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;
}
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
}