Open In App
Related Articles

Spring Boot – REST Example

Like Article
Save Article
Report issue

We all know in today’s world, most web app follows the client-server architecture. The app itself is the client or frontend part under the hood it needs to call the server or the backend to get or save the data this communication happens using HTTP protocol the same protocol is the power of the web.  On the server, we expose the bunch of services that are accessible via the HTTP protocol. The client can then directly call the services by sending the HTTP request. 

Now, this is where REST comes into the picture.  Rest stands for Representation State Transfer it is basically a convention to building these HTTP services. So we use a simple HTTP protocol principle to provide support to CREATE, READ, UPDATE & DELETE data. We refer to these operations all together called CRUD operations. In short, It is a set of rules that developers follow when they create their API. One of these rules states that you should be able to get a piece of data (called a resource) when you link to a specific URL. Let’s implement an RSET application and understand the REST approach by creating an example where we simply return the Book data in the form of JSON

Rest with Example

Set up the spring project:

So first we will set up the spring project in STS(Spring tool suite) IDE. Whose instructions have been given below

  • Click File -> New -> Project -> Select Spring Starter Project  -> Click Next.
  • A New Dialog box will open where you will provide the project-related information like project name, Java version, Maven version, and so on.
  • After that select required maven dependencies like Spring Web,  Spring Boot DevTools (Provides fast application restarts, LiveReload, and configurations for enhanced development experience.)
  • Click Finish.

Project Structure:



<?xml version="1.0" encoding="UTF-8"?>
        <relativePath/> <!-- lookup parent from repository -->
    <description>Demo project for Spring Boot</description>


Note: No need to add anything to the because we didn’t use a database. 

POJO (Plain old java object ) class:


package com.example.demo.modal;
public class Book {
    private long id;
    private String name;
    private String title;
    public Book() {
    public Book(long id, String name, String title) {
        super(); = id; = name;
        this.title = title;
    public long getId() {
        return id;
    public void setId(long id) { = id;
    public String getName() {
        return name;
    public void setName(String name) { = name;
    public String getTitle() {
        return title;
    public void setTitle(String title) {
        this.title = title;


Service interface and Service implementation class

Here we have created an interface called BookService which contains all the service methods that our application is going to provide to the user. And BookServiceImpl class that implements the BookService interface. 


package com.example.demo.service;
import java.util.HashSet;
import com.example.demo.modal.Book;
public interface BookService {
     HashSet<Book> findAllBook();
     Book findBookByID(long id);
     void addBook(Book b);
     void deleteAllData();



package com.example.demo.service;
import java.util.HashSet;
import org.springframework.stereotype.Service;
import com.example.demo.modal.Book;
public class BookServiceImpl implements BookService {
    HashSet<Book> bookList = new HashSet<Book>();
    public HashSet<Book> findAllBook() {
        if (bookList.isEmpty())
            return null;
            return bookList;
    public Book findBookByID(long id) {
        Book book = -> b.getId() == id).findAny().orElse(null);
        return book;
    public void addBook(Book b) {
    public void deleteAllData() {


Rest Controller: 

Here is the ExController class where we are exposing all the APIs which we have created. 

API’s list

  • http://localhost:8080/
    • To save the data
  • http://localhost:8080/findbyid/2
    • Find Book by id
  • http://localhost:8080/findall
    • Find all books
  • http://localhost:8080/delete
    • Delete all books


package com.example.demo.controller;
import java.util.ArrayList;
import java.util.HashSet;
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.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.modal.Book;
import com.example.demo.service.BookServiceImpl;
public class BookController {
    BookServiceImpl bookServiceImpl;
    public void addBook(@RequestBody Book book) {
    public HashSet<Book> getAllBook() {
        return bookServiceImpl.findAllBook();
    public Book geBookById(@PathVariable long id) {
        return bookServiceImpl.findBookByID(id);
    public void deleteBook() {


To run the application.


package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
public class ExApplication {
    public static void main(String[] args) {, args);


Testing APIs in Postman

Last Updated : 09 Dec, 2021
Like Article
Save Article
Share your thoughts in the comments
Similar Reads