🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
🚀 Introduction: Understanding @Controller vs @RestController
Spring Boot provides two main annotations for handling web requests:
@Controller(Used for traditional MVC applications with views).@RestController(Used for REST APIs returning JSON/XML responses).
✅ Key Differences:

1️⃣ Understanding @Controller in Spring Boot
📌 Use @Controller when building traditional web applications that return HTML views.
1. Example: Using @Controller for an MVC Web Application
@Controller
public class HomeController {
@GetMapping("/")
public String home(Model model) {
model.addAttribute("message", "Welcome to Spring Boot!");
return "home"; // Returns home.html (Thymeleaf/JSP/Freemarker)
}
}📌 How It Works:
- The method returns a view name (
home) instead of JSON. - Spring Boot resolves it to a template (
home.html). - The
Modelobject carries data to the view.
2️⃣ Understanding @RestController in Spring Boot
📌 Use @RestController when building RESTful APIs that return JSON responses.
1. Example: Using @RestController for a REST API
@RestController
@RequestMapping("/api")
public class UserController {
@GetMapping("/users")
public List<String> getUsers() {
return List.of("Ramesh", "Suresh", "Mahesh");
}
}📌 How It Works:
- The method returns JSON directly (
["Ramesh", "Suresh", "Mahesh"]). - No need for
@ResponseBody—Spring Boot automatically converts the return object to JSON.
3️⃣ How @RestController Internally Uses @ResponseBody
📌 @RestController = @Controller + @ResponseBody
Example: Using @Controller with @ResponseBody (Equivalent to @RestController)
@Controller
@RequestMapping("/api")
public class UserController {
@GetMapping("/users")
@ResponseBody // Manually specifying response type
public List<String> getUsers() {
return List.of("Ramesh", "Suresh", "Mahesh");
}
}✅ In @RestController, @ResponseBody is applied automatically, so you don’t need to add it manually.
4️⃣ Returning HTTP Responses in @RestController
📌 Use ResponseEntity<> in @RestController to customize HTTP responses.
@RestController
@RequestMapping("/api")
public class UserController {
@GetMapping("/user/{id}")
public ResponseEntity<String> getUser(@PathVariable int id) {
if (id == 1) {
return ResponseEntity.ok("User: Ramesh");
} else {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body("User not found");
}
}
}📌 How It Works:
✅ Returns 200 OK when the user exists.
✅ Returns 404 Not Found when the user does not exist.
5️⃣ When to Use @Controller vs @RestController?

@Controller vs @RestController?🎯 Summary: Best Practices for @Controller and @RestController
✅ Use @Controller for MVC applications that return views (Thymeleaf, JSP).
✅ Use @RestController for REST APIs that return JSON responses.
✅ Use @ResponseBody if you need JSON responses inside @Controller.
✅ Use ResponseEntity<> in @RestController to customize HTTP responses.
✅ Follow REST API best practices for building scalable microservices.
🚀 Following these best practices ensures a clean and maintainable Spring Boot application!
📢 Stay Connected & Keep Learning! 🚀
🔗 Explore my Udemy courses: Java Guides Udemy Courses
📖 Read more tutorials on my blog: Java Guides
🎥 Watch free Java video tutorials on YouTube: Java Guides YouTube
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment