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.
Why Do We Need @RestController?
@Controller
public class HomeController {
@GetMapping("/home")
public String home() {
return "home";
}
}home.jsp
or
home.htmlHowever, REST APIs usually return data instead of views.
@RestController
What Does @RestController Do?
@RestController
@Controller
@ResponseBodyHow 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";
}
}GET /hello
Welcome to Spring Boot
Returning Objects
@RestController
public class EmployeeController {
@GetMapping("/employee")
public Employee getEmployee() {
return new Employee(
101,
"John");
}
}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;
}
}{
"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.
Suppose we are building an Employee Management System.
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
@RestController
public class UserController {
}@Controller
@ResponseBody
public class UserController {
}Benefits of @RestController
Real-World Example
GET /accounts
Returns account details as JSON.
POST /accounts
Creates a new account.
The controller handling these requests would typically be annotated with:
@RestController
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?