Starting with Spring Boot Rest

Abhishek Singh
4 min readAug 28, 2022


Spring Boot provides a very good support to building RESTful Web Services for enterprise applications. In this post we will create simple RESTful web services using Spring Boot.

For building a RESTful Web Services, we need to add the Spring Boot Starter Web dependency into the pom.xml file.


<?xml version=”1.0" encoding=”UTF-8"?>

<project xmlns=”"








<relativePath /> <! — lookup parent from repository →






<description>Spring Boot Demo Project</description>






























Before we go ahead to build a RESTful web service, it is important that we have knowledge of the following annotations −

Rest Controller

The @RestController annotation is used to define the RESTful web services. It serves JSON, XML and custom response. Its syntax is shown below −

public class EmployeeController {

Request Mapping

The @RequestMapping annotation is used to define the Request URI to access the REST Endpoints. We can define Request method to consume and produce object. The default request method is GET.

public ResponseEntity<Object> getEmployees() { }

Request Body

The @RequestBody annotation is used to define the request body content type.

public ResponseEntity<Object> createEmployee(@RequestBody Product product) {

Path Variable

The @PathVariable annotation is used to define the custom or dynamic request URI. The Path variable in request URI is defined as curly braces {} as shown below −

public Employee getEmployee(@PathVariable("empNo") String empNo) {
System.out.println("Data Fetched Successfully");return employeeDao.getEmployee(empNo);

Request Parameter

The @RequestParam annotation is used to read the request parameters from the Request URL. By default, it is a required parameter. We can also set default value for request parameters as shown here −

public ResponseEntity<Object> getEmployee(
@RequestParam(value = "name", required = false, defaultValue = "honey") String name) {

Lets go back to create a simple Employee Spring boot project using various Http request methods.

Note : Here we are using custom java classes to have data using collection we are not using spring-boot-starter-jpa

Here we have a simple model class

package com.abhishek.spring.boot.model;

import org.springframework.stereotype.Component;


public class User {

private int id;

private String firstName;

private String lastName;

private String email;

private String country;

public User() {


public User(int id, String firstName, String lastName, String email, String country) {

super(); = id;

this.firstName = firstName;

this.lastName = lastName; = email; = country;


public int getId() {

return id;


public String getFirstName() {

return firstName;


public String getLastName() {

return lastName;


public String getEmail() {

return email;


public String getCountry() {

return country;


public void setId(int id) { = id;


public void setFirstName(String firstName) {

this.firstName = firstName;


public void setLastName(String lastName) {

this.lastName = lastName;


public void setEmail(String email) { = email;


public void setCountry(String country) { = country;



public String toString() {

return “User [id=” + id + “, firstName=” + firstName + “, lastName=” + lastName + “, email=” + email

+ “, country=” + country + “]”;



Here we have simple java class to have business logic

package com.abhishek.spring.boot.dao;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.abhishek.spring.boot.model.User;

public class UserDao {

private static final List<User> userList = new ArrayList<User>();

static {

private static void createUser() {

User user1 = new User(1, “Heidi”, “Pitts”, “”, “Costa Rica”);
User user2 = new User(2, “Daphne”, “Workman”, “”, “UK”);
User user3 = new User(3, “Jemima”, “Duffy”, “”, “Colombia”);
User user4 = new User(4, “Roary”, “Berg”, “”, “US”);
User user5 = new User(5, “Byron”, “Benjamin”, “”, “Germany”);
User user6 = new User(6, “Hermann”, “Pardon”, “”, “Poland”);
User user7 = new User(7, “Rossie”, “Giovannini”, “”, “Russia”);
User user8 = new User(8, “Killian”, “Brotherheed”, “”, “Indonesia”);
User user9 = new User(9, “Marga”, “Tice”, “”, “Spain”);
User user10 = new User(10, “Khalil”, “Edelheit”, “”, “China”);
User user11 = new User(11, “Rebecka”, “Bavage”, “”, “Azerbaijan”);
User user12 = new User(12, “Athene”, “Errigo”, “”, “Canada”);
User user13 = new User(13, “Jarrod”, “Sterling”, “”, “Sweden”);
User user14 = new User(14, “Robinet”, “Furmonger”, “”, “Portugal”);
User user15 = new User(15, “Abhishek”, “Singh”, “”, “Tanzania”);
User user16 = new User(16, “Filia”, “Withrington”, “”, “Bulgaria”);
User user17 = new User(17, “Padget”, “Cornish”, “”, “Belarus”);
User user18 = new User(18, “Olivie”, “Isabell”, “”, “Greece”);
User user19 = new User(19, “John”, “Partlett”, “”, “Argentina”);
User user20 = new User(20, “Tabbie”, “Cartan”, “”, “Singapore”);


public User getUser(int id) {
return userList.get(id);

public User addUser(User user) {
return user;

public User updateUser(User user) {
userList.set(user.getId(), user);
return user;

public void deleteUser(int id) {

public List<User> getAllUsers() {
Collection<User> c =;
List<User> list = new ArrayList<User>();
return list;

And now we have a rest controller to expose api to the end users

package com.abhishek.spring.boot.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.abhishek.spring.boot.dao.UserDao;
import com.abhishek.spring.boot.model.User;

public class UserController {

private UserDao userDao;

public List<User> getUsers() {
List<User> list = userDao.getAllUsers();
System.out.println(“All Users Fetched Successfully”);
return list;

public User getUser(@PathVariable(“userId”) int id) {
System.out.println(“User Fetched Successfully”);
return userDao.getUser(id);

public User addUser(@RequestBody User user) {
System.out.println(“Creating User: “ + user.getId());
return userDao.addUser(user);

public User updateUser(@RequestBody User user) {
System.out.println(“(Updating User: “ + user.getId());
return userDao.updateUser(user);

public void deleteUser(@PathVariable(“userId”) int id) {
System.out.println(“Deleting User: “ + id);
System.out.println(“User Deleted Successfully”);


Thanks for reading…Keep Learning