What does the @RestController annotation do in Spring Boot?

The @RestController annotation is used in Spring Boot to create RESTful web services. It tells Spring that the class will handle HTTP requests and that the return value of each method should be sent directly in the HTTP response body, typically as JSON or XML, instead of rendering a view.

Key Points

Why Do We Need @RestController?

In traditional Spring MVC applications

Example

@Controller
public class HomeController {

    @GetMapping("/home")
    public String home() {

        return "home";
    }
}

Here, Spring searches for a view named

home.jsp

or

home.html

However, REST APIs usually return data instead of views.

For this purpose, Spring Boot provides

@RestController

What Does @RestController Do?

Internally

@RestController

is equivalent to

@Controller
@ResponseBody

This means

How Response Conversion Works

Client Request

|

|

@RestController

|

Java Object Returned

|

Jackson Converter

|

JSON Response

|

Client

Spring automatically converts Java objects into JSON using the Jackson library.

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class EmployeeController {

    @GetMapping("/hello")
    public String hello() {

        return "Welcome to Spring Boot";
    }
}

Request

GET /hello

Response

Welcome to Spring Boot

Returning Objects

@RestController
public class EmployeeController {

    @GetMapping("/employee")
    public Employee getEmployee() {

        return new Employee(
                101,
                "John");
    }
}

Employee Class

public class Employee {

    private int id;
    private String name;

    public Employee(
            int id,
            String name) {

        this.id = id;
        this.name = name;
    }

    public int getId() {

        return id;
    }

    public String getName() {

        return name;
    }
}

JSON Response

{

"id": 101,

name": "John

}

Spring automatically converts the object into JSON.

Common HTTP Operations

GET

Retrieve data.

@GetMapping("/employees")

POST

Create data.

@PostMapping("/employees")

PUT

Update data.

@PutMapping("/employees/{id}")

DELETE

Delete data.

@DeleteMapping("/employees/{id}")

These methods are commonly used inside @RestController classes.

Example

Suppose we are building an Employee Management System.

Endpoints

GET /employees

Returns employee list.

POST /employees

Creates a new employee.

PUT /employees/101

Updates employee details.

DELETE /employees/101

Deletes an employee.

All these endpoints are typically implemented inside a @RestController.

@RestController vs @Controller

@RestController

@Controller

Example

@RestController

public class UserController {

}

Equivalent To

@Controller
@ResponseBody

public class UserController {

}

Benefits of @RestController

Real-World Example

In a banking application

GET /accounts

Returns account details as JSON.

POST /accounts

Creates a new account.

The controller handling these requests would typically be annotated with:

@RestController

Interview Tip

A concise interview answer is

@RestController is a Spring Boot annotation used to create RESTful web services. It combines @Controller and @ResponseBody, meaning all methods return data directly in the HTTP response body rather than resolving views. Spring automatically converts returned objects into JSON or XML, making it ideal for building REST APIs and microservices.

Last updated: 11 November 2026

Previous question: How does an HTTPS request flow through a Spring Boot application?

Next question: What is the difference between @Controller and @RestController?

More Spring Boot - Level I interview questions

All Spring Boot - Level I questions