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: • @RestController is a specialized controller for REST APIs. • It combines the functionality of @Controller and @ResponseBody. • Method return values are automatically converted into JSON or XML. • It is commonly used for building RESTful web services and microservices. • Works seamlessly with HTTP methods such as GET, POST, PUT, and DELETE.
Why Do We Need @RestController?
In traditional Spring MVC applications:
• Controllers return view names. • Spring resolves JSP, Thymeleaf, or HTML pages.
Example:
@Controller
public class HomeController {
@GetMapping("/home")
public String home() {
return "home";
}
}Here, Spring searches for a view named:
home.jsp
or
home.htmlHowever, 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
@ResponseBodyThis means:
• The class acts as a controller. • All method responses are written directly to the HTTP response body.
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.
Code Example:
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
Code Example:
@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• Used for REST APIs. • Returns JSON/XML data. • Automatically includes @ResponseBody.
@Controller
• Used for MVC applications. • Returns view names. • Requires @ResponseBody for REST responses.
Example:
@RestController
public class UserController {
}Equivalent To:
@Controller
@ResponseBody
public class UserController {
}Benefits of @RestController
• Less boilerplate code • Automatic JSON conversion • Ideal for REST APIs • Simplified development • Better support for microservices architecture
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."