Difference Between @Controller and @RestController in Spring Boot

 ðŸš€ 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 Model object 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?

When to Use @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

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare