Spring Boot @Controller vs @RestController: UI vs APIs

❓ @Controller vs @RestController in Spring Boot   Same name. Different output. Day 33 of Mastering Backend This is one of the most common Spring Boot confusions — and once you get it, you’ll never mix them up again. 🧠 @Controller Use this when you are building server-side rendered applications. • Returns views (HTML, JSP, Thymeleaf) • Works with ViewResolvers • You usually return a page name Example: @Controller public class PageController {   @GetMapping("/home")   public String home() {     return "home"; // returns home.html   } } 👉 Response = HTML page ( Frontend View Page) 🧠 @RestController Use this when you are building REST APIs. • Returns data directly • Automatically converts objects to JSON • No view resolution involved Example: @RestController public class UserController {   @GetMapping("/users")   public List<User> getUsers() {     return users;   } } 👉 Response = JSON data 🔑 Key difference (easy to remember) @Controller → UI pages   @RestController → APIs  And remember: @RestController = @Controller + @ResponseBody 🎯 When to use what? • Building HTML pages → use @Controller • Building REST APIs → use @RestController 💾 Save this for interviews   🔁 Repost if this cleared the confusion I’m sharing everything that confused me while learning Java, Spring Boot, Microservices, System Design and Data Structures & Algorithms. Rewriting it in a way that finally makes sense. If you’re a curious developer like me and want fewer “why is this happening?” moments in tech,   you’ll probably enjoy what’s coming next. 𝗜𝗳 𝘁𝗵𝗶𝘀 𝗵𝗲𝗹𝗽𝗲𝗱 𝘆𝗼𝘂 𝘀𝗲𝗲 𝗝𝗮𝘃𝗮 𝗱𝗶𝗳𝗳𝗲𝗿𝗲𝗻𝘁𝗹𝘆 &   𝗜𝗳 𝘆𝗼𝘂 𝘄𝗮𝗻𝘁 𝘁𝗼 𝗴𝗿𝗼𝘄 𝗰𝗼𝗻𝘀𝗶𝘀𝘁𝗲𝗻𝘁𝗹𝘆 𝘄𝗶𝘁𝗵 𝗺𝗲 📈📈  𝗜 𝘀𝗵𝗼𝘄 𝘂𝗽 𝗱𝗮𝗶𝗹𝘆, 𝐋𝐢𝐤𝐞 𝐚𝐧𝐝 𝐅𝐨𝐥𝗹𝗼𝘄 ❤️  𝐇𝐚𝐩𝐩𝐲 𝐭𝐨 𝐜𝐨𝐧𝗻𝗲𝐜𝐭 𝐰𝐢𝐭𝐡 𝐞𝗻𝗴𝗶𝗻𝗲𝗲𝗿𝘀   𝐰𝗵𝗼 𝐞𝗻𝗷𝗼𝘆 𝗹𝗲𝗮𝗿𝗻𝗶𝗻𝗴, 𝗯𝘂𝗶𝗹𝗱𝗶𝗻𝗴, 𝐚𝗻𝗱 𝐠𝗿𝗼𝘄𝗶𝗻𝗴 ❤️  #Java   #SpringBoot   #Microservices #SystemDesign #DataStructures #CleanCode   #LearnInPublic

  • graphical user interface, text, application

Thanks For sharing This 💯

To view or add a comment, sign in

Explore content categories