Spring Boot @Controller vs @RestController: Views vs Data

🚀 @Controller vs @RestController in Spring Boot While learning Spring Boot, I came across two annotations that looked very similar: @Controller and @RestController At first, I thought they were the same… but they are used for different purposes. 🔹 @Controller Used in Spring MVC applications Returns HTML views (web pages) Often used with Thymeleaf or JSP Example: @Controller public class HomeController {    @GetMapping("/home")    public String home() {        return "home";    } } This returns an HTML page named home. 🔹 @RestController Used for REST APIs Returns JSON or data directly Common in backend services Example: @RestController public class UserController {    @GetMapping("/users")    public List<User> getUsers() {        return userService.getAllUsers();    } } This returns data in JSON format instead of a web page. 💡 Simple Way to Remember @Controller → Returns Views (HTML) @RestController → Returns Data (JSON) If you are building APIs for React, mobile apps, or frontend applications, you will mostly use @RestController. #Java #SpringBoot #BackendDevelopment #LearningInPublic #TechJourney

  • diagram

To view or add a comment, sign in

Explore content categories