When building web applications using Spring Boot, three commonly used annotations are @RequestMapping, @RestController, and @Controller. These annotations define how your application handles HTTP requests and sends responses back to the client. Understanding them clearly is very important for both real-world development and interviews.
What is @Controller?
@Controller is used to mark a class as a Spring MVC controller that returns a view (HTML page) as a response.
This annotation is mainly used in traditional web applications where the server returns a frontend page.
Real-world example:
Consider a banking website. When a user logs in, the system verifies the details and then returns a dashboard page. This is handled using a controller.
Code example:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HomeController {
@RequestMapping("/home")
public String homePage() {
return "home"; // returns home.html
}
}
Use our Online Code Editor
Here, /home returns a UI page named home.html.
What is @RestController?
@RestController is a specialized version of @Controller that returns data directly in JSON or XML format instead of a view."
It is widely used in REST APIs where the frontend and backend are separate.
Real-world example:
In an e-commerce mobile app, when you open the app and see products, the data comes from backend APIs in JSON format.
Code example:
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
@RestController
public class UserController {
@RequestMapping("/user")
public String getUser() {
return "User data returned";
}
}
Use our Online Code Editor
This returns raw data instead of an HTML page.
What is @RequestMapping?
@RequestMapping is used to map HTTP requests to specific handler methods or classes.
It connects URLs with the correct method in your application.
Real-world example:
A website has multiple pages, like /login, /profile, /orders. Each URL is mapped to a different method using @RequestMapping.
Code example:
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
public class ProductController {
@RequestMapping("/products")
public String getProducts() {
return "List of products";
}
}
Use our Online Code Editor
The final endpoint becomes /api/products.
Additional Concepts (Important for Interviews)
1. Difference between @Controller and @RestController
@RestController = @Controller + @ResponseBody
This means @RestController automatically converts return values into JSON format.
In @Controller, if you want to return JSON, you must use @ResponseBody manually.
Example:
@Controller
public class DemoController {
@RequestMapping("/data")
@ResponseBody
public String getData() {
return "Data response";
}
}
Use our Online Code Editor
2. HTTP Method Specific Mapping
Instead of using @RequestMapping everywhere, Spring provides shortcuts:
@GetMapping→ for GET requests@PostMapping→ for POST requests@PutMapping→ for UPDATE@DeleteMapping→ for DELETE
Example:
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/users")
public class UserApi {
@GetMapping
public String getUsers() {
return "All users";
}
@PostMapping
public String createUser() {
return "User created";
}
}
Use our Online Code Editor
3. Path Variables and Request Parameters
These are commonly asked in interviews.
Example:
@GetMapping("/users/{id}")
public String getUserById(@PathVariable int id) {
return "User id is " + id;
}
Use our Online Code Editor
Key Differences Summary
@Controller→ Returns HTML view@RestController→ Returns JSON/data@RequestMapping→ Maps URL to method
Difference between @Controller and @RestController
Basic Definition
@Controller is used to return a view (HTML page) from the server.
@RestController is used to return data (JSON or XML) directly from the server."
Main Difference
@Controller -> Used for web applications (returns UI pages)
@RestController -> Used for REST APIs (returns data)
Real-world Example
@Controller:
Used in websites like banking portals, where a dashboard page is returned after login.
@RestController:
Used in mobile or frontend applications where backend sends data in JSON format.
Code Examples
@Controller Example:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class PageController {
Use our Online Code Editor
@RequestMapping("/dashboard")
public String dashboard() {
return "dashboard"; // returns dashboard.html
}
Use our Online Code Editor
@RestController Example:
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
@RestController
public class ApiController {
Use our Online Code Editor
@RequestMapping("/data")
public String getData() {
return "This is API response";
}
Use our Online Code Editor
Internal Working
@RestController = @Controller + @ResponseBody
This means @RestController automatically returns data, while in @Controller you need to use @ResponseBody manually.
When to Use
Use @Controller when building frontend-based applications (HTML, JSP, Thymeleaf).
Use @RestController when building backend APIs (used by mobile apps or frontend frameworks like React or Angular).
Conclusion
These annotations are the foundation of Spring Boot web development. If you understand when to use @Controller and when to use @RestController, you can design both UI-based and API-based applications easily. @RequestMapping and its variants help you organize your endpoints properly.
For interviews, always remember the definitions, differences, and real-world usage. Strong basics in these concepts will help you write better code and explain your knowledge clearly.
Have a great one!!!
Author: Ayush Shrivastava
Thank you for being a part of the community
Before you go:
Whenever you’re ready
There are 4 ways we can help you become a great backend engineer:
- The MB Platform: Join thousands of backend engineers learning backend engineering. Build real-world backend projects, learn from expert-vetted courses and roadmaps, track your learning and set schedules, and solve backend engineering tasks, exercises, and challenges.
- The MB Academy: The “MB Academy” is a 6-month intensive Advanced Backend Engineering Boot Camp to produce great backend engineers.
- Join Backend Weekly: If you like posts like this, you will absolutely enjoy our exclusive weekly newsletter, sharing exclusive backend engineering resources to help you become a great Backend Engineer.
- Get Backend Jobs: Find over 2,000+ Tailored International Remote Backend Jobs or Reach 50,000+ backend engineers on the #1 Backend Engineering Job Board.

Top comments (0)