@RestController and @RequestMapping in Spring Boot

🚀 DAY 14 — @RestController & @RequestMapping (CORE API CONCEPT 🔥) ✅ 1. CLEAR CONCEPT (VERY IMPORTANT) 👉 @RestController = Used to create REST APIs 👉 It combines: @Controller @ResponseBody 💡 Means: 👉 Return data (JSON), not HTML 👉 @RequestMapping = Maps URL to method/class 💡 Means: 👉 Which URL → which method 🧠 SIMPLE UNDERSTANDING 👉 Client calls API → Controller handles it 💻 EXAMPLE (VERY IMPORTANT) @RestController @RequestMapping("/api") public class UserController { @RequestMapping("/hello") public String hello() { return "Hello World"; } } 👉 Output: http://localhost:8080/api/hello ⚡ SHORTCUT ANNOTATIONS (IMPORTANT 🔥) Instead of @RequestMapping, use: @GetMapping @PostMapping @PutMapping @DeleteMapping ✔ More readable ✔ Mostly used in real projects 🔄 FLOW (VERY IMPORTANT) 👉 Client → Controller → Method → Response 🎯 INTERVIEW QUESTIONS (MUST KNOW 🔥) ❓ What is @RestController? 👉 Used to create REST APIs (returns JSON data) ❓ Difference: @Controller vs @RestController? 👉 @Controller → returns view (HTML) 👉 @RestController → returns data (JSON) ❓ What is @RequestMapping? 👉 Maps URL to controller method ❓ Can we use @RequestMapping at class level? 👉 Yes (for base URL) ❓ Why use @GetMapping instead of @RequestMapping? 👉 Short, clean, specific to HTTP method 📂 REAL USE CASE 👉 /api/users → get users 👉 /api/users/add → add user 💡 FINAL UNDERSTANDING 👉 @RestController = API creator 👉 @RequestMapping = URL mapping 💬 Did you try your first API yet? Day 14 done ✅ #SpringBoot #Java #BackendDevelopment #LearningInPublic #30DaysOfCode #Developers

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories