Spring Boot @Controller Annotation Explained

📌 Spring Boot Annotation Series – Part 11 ✅ @Controller annotation The @Controller annotation is used to mark a class as a web controller in Spring MVC 👇 🔹 What is @Controller? @Controller is a specialization of @Component. It tells Spring: 👉 “This class will handle web requests.” 🔹 Why do we use @Controller? To handle HTTP requests To return web pages (views) To separate web layer from business logic 🔹 How does it work? When a request comes to the application: Spring maps the URL to a method in the controller The method processes the request Returns a view name (like an HTML page) 🔹 Simple example @Controller public class HomeController { @GetMapping("/home") public String home() { return "home"; // returns home.html } } 🔹 Important Note @Controller is mainly used for MVC applications that return views. If you want to return JSON responses (REST APIs), we usually use: 👉 @RestController 🔹 In simple words @Controller is used to handle web requests and return web pages. 👉 🧠 Quick Understanding @Controller handles web requests Returns view (HTML/JSP/Thymeleaf) Used in Spring MVC applications #SpringBoot #Java #ControllerAnnotation #BackendDevelopment #LearningInPublic

To view or add a comment, sign in

Explore content categories