Open In App

Spring - REST Controller

Last Updated : 12 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Spring Boot is built on top of the Spring and contains all the features of spring. Spring Boot is a popular framework for building microservices and RESTful APIs due to its rapid setup and minimal configuration requirements. When developing REST APIs in Spring, the @RestController annotation plays an important role in handling HTTP requests and returning JSON responses.

In this article, we will discuss the Rest Controller in Spring Boot, the @RestController annotation, its differences from @Controller, and a step-by-step guide to implementing a RESTful web service in Spring Boot.

@RestController Annotation in Spring Boot

Spring provides two main controller annotations:

  • @Controller annotation is used for MVC-based applications where responses are typically HTML pages.
  • @RestController annotation is a specialized version of @Controller that automatically serializes return objects into JSON/XML responses. It is equivalent to @Controller + @ResponseBody.

This annotation is used at the class level and allows the class to handle the requests made by the client. The RestController allows the class to handle REST API requests such as GET, POST, PUT, and DELETE by automatically serializing responses to JSON.

Step-by-Step Implementation

Step 1: Create a Spring Boot Project

Use Spring Initializr to generate a new Spring Boot project.

Project Configuration:

  • Project Type: Maven
  • Language: Java
  • Spring Boot Version: 3.x (Latest Version)
  • Dependencies: Spring Web
  • Java Version: 17+

Click on Generate, which will download the starter project.

Spring-Initializr


Step 2: Import the Spring Boot Project in IDE

Extract the zip file. Now open a suitable IDE (here we are using IntelliJ IDEA) and then go to File > New > Project from Existing Sources and then select pom.xml. Click on import changes on prompt and wait for the project to sync as pictorially depicted below as follows:

Note: In the Import Project for Maven window, make sure you choose the same version of JDK which you selected while creating the project.

Step 3: Create the Model Class

Go to src > main > java, create a java class with the name Details.

Details.java:

Java
public class Details {
    
    private int id;
    private String name;

    // Constructor
    public Details(int id, String name) {
        this.id = id;
        this.name = name;
    }

    // Getters and Setters
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}



Now create another Java class with the name Controller and add the annotation @RestController.

Controller.java:

Java
@RestController
@RequestMapping("/api")
public class Controller {

    private List<Details> detailsList = new ArrayList<>();

    // GET API to fetch all details
    @GetMapping("/details")
    public List<Details> getAllDetails() {
        return detailsList;
    }

    // POST API to add new details
    @PostMapping("/details")
    public String addDetails(@RequestBody Details details) {
        detailsList.add(details);
        return "Data Inserted Successfully";
    }

    // DELETE API to remove an entry by ID
    @DeleteMapping("/details/{id}")
    public String deleteDetails(@PathVariable int id) {
        detailsList.removeIf(details -> details.getId() == id);
        return "Data Deleted Successfully";
    }
}

This application is now ready to run.

Step 4: Run the Application

Run the SpringBootApplication class to start the embedded Tomcat server.

Output


Note: The default port of the Tomcat server is 8080 and can be changed in the application.properties file.

Step 5: Test APIs using Postman

1. Add a New Detail (POST Request)

Endpoint: POST http://localhost:8080/api/details

Request Body:

{

"id": 1,

"name": "John Doe"

}

Response:

"Data Inserted Successfully"

2. Fetch All Details (GET Request)

Endpoint: GET http://localhost:8080/api/details

Response:

[

{

"id": 1,

"name": "John Doe"

}

]

3. Delete a Detail (DELETE Request)

Endpoint: DELETE http://localhost:8080/api/details/1

Response:

"Data Deleted Successfully"

To know how to use Postman, refer: How to test Rest APIs in Postman


Next Article
Practice Tags :

Similar Reads