❓ @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
Spring Boot @Controller vs @RestController: UI vs APIs
More Relevant Posts
-
🚀 Spring Boot Developers — stop memorizing. Start recognizing. Most Spring Boot bugs don’t come from missing annotations. They come from not knowing which annotation does what under the hood. So I put together a battle-tested Spring Boot Annotation Cheatsheet covering 70+ essential annotations across 10 real-world categories 👇 --- 📌 What you’ll master (and stop Googling forever): 🧩 Component Scanning & DI → @Autowired, @Qualifier, @Primary (and when each one actually matters) ⚙️ Configuration & Beans → @Configuration, @Bean, @Lazy 🌐 Spring MVC & REST → All HTTP mapping annotations that power real APIs 🗄️ Spring Data JPA → Entity mappings, relationships, and persistence essentials 🔐 Spring Security → @PreAuthorize, @Secured, @RolesAllowed 🧬 Microservices → @FeignClient, @EnableEurekaClient, @CircuitBreaker 🧪 Testing → @SpringBootTest, @MockBean, @DataJpaTest 🎭 AOP → @Aspect, @Before, @After, @Around ⚡ Caching → @Cacheable, @CachePut, @CacheEvict ✅ Validation → @NotBlank, @Email, @Pattern --- 💡 Reality check: Framework mastery isn’t about using more annotations. It’s about using the right one at the right layer. Bookmark this. Your future self debugging at 2 AM will thank you. 👇 Which Spring Boot annotation do you use the most — and why? Let’s compare notes. 🔁 Repost if this helps your Java circle ➕ Follow Pondurai Madheswaran for daily Java & Spring insights #PonduraiWrites #SpringBoot #Java #BackendDevelopment #SoftwareEngineering #SpringFramework #JavaDevelopment #Microservices #DeveloperTips
To view or add a comment, sign in
-
🚀 Spring Boot Developers — stop memorizing. Start recognizing. Most Spring Boot bugs don’t come from missing annotations. They come from not knowing which annotation does what under the hood. So I put together a battle-tested Spring Boot Annotation Cheatsheet covering 70+ essential annotations across 10 real-world categories 👇 --- 📌 What you’ll master (and stop Googling forever): 🧩 Component Scanning & DI → @Autowired, @Qualifier, @Primary (and when each one actually matters) ⚙️ Configuration & Beans → @Configuration, @Bean, @Lazy 🌐 Spring MVC & REST → All HTTP mapping annotations that power real APIs 🗄️ Spring Data JPA → Entity mappings, relationships, and persistence essentials 🔐 Spring Security → @PreAuthorize, @Secured, @RolesAllowed 🧬 Microservices → @FeignClient, @EnableEurekaClient, @CircuitBreaker 🧪 Testing → @SpringBootTest, @MockBean, @DataJpaTest 🎭 AOP → @Aspect, @Before, @After, @Around ⚡ Caching → @Cacheable, @CachePut, @CacheEvict ✅ Validation → @NotBlank, @Email, @Pattern --- 💡 Reality check: Framework mastery isn’t about using more annotations. It’s about using the right one at the right layer. Bookmark this. Your future self debugging at 2 AM will thank you. 👇 Which Spring Boot annotation do you use the most — and why? Let’s compare notes. 🔁 Repost if this helps your Java circle ➕ Follow Pondurai Madheswaran for daily Java & Spring insights #PonduraiWrites #SpringBoot #Java #BackendDevelopment #SoftwareEngineering #SpringFramework #JavaDevelopment #Microservices #DeveloperTips
To view or add a comment, sign in
-
What Actually Happens When We Use @RestController in Spring Boot? When I first saw the annotation @RestController, I honestly didn’t know why it was used. Everyone was using it ,So I started using too. But one day I paused and asked myself: What is Spring actually doing when I write @RestController? That’s when things slowly started making sense. In Spring , there are two common annotations we used with Controllers: 1.@Controller 2.@RestController At first, they look almost the same. But they behave very differently. ->@Controller When we use @Controller , Spring assumes we are returning a view. That means : It expects an HTML page. This is mostly used in traditional Spring MVC applications where the backend returns web pages. -> @RestController When we use @RestController, we are telling Spring: i . Don’t return a view. ii . Return the data directly in the response body. Internally, @RestController is simply a combination of: @Controller + @ResponseBody So instead of rendering HTML , Spring sends JSON directly to the client. That’s why in REST APIs, we use @RestController. Now when I use @RestController, I understand what it’s telling Spring to do. Understanding little things like this , changing how I see backend development. More tomorrow 🚀 #SpringBoot #Java #BackendDevelopment #LearningInPublic #SoftwareEngineering
To view or add a comment, sign in
-
-
🚀Day : 02/100 Spring Boot Annotations – Quick Reference for Backend Developers If you are learning Spring Boot / Java Backend Development, understanding annotations is very important. These annotations help us reduce boilerplate code and build powerful REST APIs quickly. Here are some commonly used Spring Boot annotations every developer should know: 🔹 Stereotype Annotations • "@Component" – Generic Spring Bean • "@Service" – Business Logic Layer • "@Repository" – Database Layer • "@Controller" – MVC Controller • "@RestController" – REST API Controller 🔹 REST API Annotations • "@RequestMapping" – Map HTTP requests • "@GetMapping" – Handle GET request • "@PostMapping" – Handle POST request • "@PutMapping" – Handle PUT request • "@DeleteMapping" – Handle DELETE request • "@PathVariable" – URL parameter • "@RequestBody" – Request data binding 🔹 Dependency Injection • "@Autowired" – Automatically inject dependency • "@Qualifier" – Specify bean • "@Inject" – JSR-330 injection 🔹 JPA / Database • "@Entity" – Database entity • "@Table" – Table name • "@Id" – Primary key • "@GeneratedValue" – Auto generated ID • "@Column" – Column mapping 🔹 Validation • "@Valid" • "@NotNull" • "@NotBlank" • "@Size" • "@Email" 💡 Mastering these annotations makes Spring Boot development faster, cleaner, and production-ready. #SpringBoot #Java #BackendDeveloper #JavaDeveloper #RESTAPI #SpringFramework #Programming #SoftwareDevelopment #Coding #Developers
To view or add a comment, sign in
-
-
💬 A friend asked me to check his Spring Boot API today — it kept returning 400 Bad Request. The JSON looked correct. The endpoint looked fine. Still failing. After 2 minutes, I spotted the issue 👇 He forgot to add @RequestBody in the controller method. @PostMapping("/users") public ResponseEntity<?> createUser(@RequestBody User user) { return ResponseEntity.ok(service.save(user)); } 👉 Why this matters? Spring Boot doesn’t automatically convert incoming JSON into Java objects. @RequestBody tells Spring: “Take the HTTP request body → deserialize JSON → map it to this object.” Without it: ❌ Object stays null ❌ Request mapping fails ❌ You get 400 Bad Request Small annotation… but critical for REST APIs. Moments like this remind me — backend development is often about understanding how the framework works internally, not just writing code. Learning something new every day with Spring Boot 🚀 #Java #SpringBoot #BackendDeveloper #FullStackDeveloper #RESTAPI #CodingLife #BugFix #SoftwareEngineering #Developers #TechLearning
To view or add a comment, sign in
-
🚀 What Really Happens When You Mark a Class with @Component in Spring? Many developers use @Component daily… But as a Java developer, we should understand what happens behind the scenes. When you write: @Component public class RedisCacheService { } ✅ What Spring Actually Does 1️⃣ During component scanning, Spring detects the class. 2️⃣ It creates a BeanDefinition. 3️⃣ Registers it inside the ApplicationContext. 4️⃣ The bean is managed by the IoC container. 👉 Yes, ApplicationContext is the implementation of the IoC container. 🔎 Is @Component Bean Singleton? Important clarification: @Component @Service @Repository @Controller @Configuration ✅ All are Singleton by default Unless explicitly defined: @Scope("prototype") Default scope in Spring = Singleton 🏷 Default Bean Naming Strategy If you write: @Component public class RedisCacheService { } Spring registers the bean as: redisCacheService 👉 Lower camel case of class name (Handled internally by AnnotationBeanNameGenerator) If you write: @Component("redis") Now bean name becomes: redis 🏗 Why Naming Matters in Architecture? In large systems, naming is not small thing. Example: @Component("redis") public class RedisCacheService implements CacheService { } @Component("inMemory") public class InMemoryCacheService implements CacheService { } Now we can control injection using: @Qualifier("redis") This supports: ✔ Loose Coupling ✔ Strategy Pattern ✔ Open/Closed Principle ✔ Plug-and-play architecture Good naming = Clean Infrastructure Design. ⚡ @Component vs @Configuration Both are singleton. But @Configuration is special: It uses proxy (CGLIB) to maintain singleton behavior of @Bean methods internally. That is container-level intelligence. 🎯 Final Thought Using Spring is easy. Understanding Spring container behavior is architecture. Stop only writing annotations. Start thinking about: Bean lifecycle BeanDefinition Naming strategy Scope design Dependency resolution That’s how you grow from Developer → Architect. #Java #SpringBoot #Backend #Architecture #IoC #CleanCode
To view or add a comment, sign in
-
🚀 𝗧𝗼𝗽 𝟱 𝗦𝗽𝗿𝗶𝗻𝗴 𝗕𝗼𝗼𝘁 𝗔𝗻𝗻𝗼𝘁𝗮𝘁𝗶𝗼𝗻𝘀 𝗘𝘃𝗲𝗿𝘆 𝗗𝗲𝘃𝗲𝗹𝗼𝗽𝗲𝗿 𝗠𝗨𝗦𝗧 𝗞𝗻𝗼𝘄 If you're working with Spring Boot, these annotations are not optional… they are essential. Mastering them will instantly make your code cleaner, more readable, and production-ready 💯 🔥 𝟭. @𝗥𝗲𝘀𝘁𝗖𝗼𝗻𝘁𝗿𝗼𝗹𝗹𝗲𝗿 👉 Combines @Controller + @ResponseBody ✔ 𝘜𝘴𝘦𝘥 𝘵𝘰 𝘣𝘶𝘪𝘭𝘥 𝘙𝘌𝘚𝘛 𝘈𝘗𝘐𝘴 ✔ 𝘈𝘶𝘵𝘰𝘮𝘢𝘵𝘪𝘤𝘢𝘭𝘭𝘺 𝘤𝘰𝘯𝘷𝘦𝘳𝘵𝘴 𝘑𝘢𝘷𝘢 𝘰𝘣𝘫𝘦𝘤𝘵𝘴 → 𝘑𝘚𝘖𝘕 @𝘙𝘦𝘴𝘵𝘊𝘰𝘯𝘵𝘳𝘰𝘭𝘭𝘦𝘳 @𝘙𝘦𝘲𝘶𝘦𝘴𝘵𝘔𝘢𝘱𝘱𝘪𝘯𝘨("/𝘶𝘴𝘦𝘳𝘴") 𝘱𝘶𝘣𝘭𝘪𝘤 𝘤𝘭𝘢𝘴𝘴 𝘜𝘴𝘦𝘳𝘊𝘰𝘯𝘵𝘳𝘰𝘭𝘭𝘦𝘳 { @𝘎𝘦𝘵𝘔𝘢𝘱𝘱𝘪𝘯𝘨 𝘱𝘶𝘣𝘭𝘪𝘤 𝘓𝘪𝘴𝘵<𝘜𝘴𝘦𝘳> 𝘨𝘦𝘵𝘈𝘭𝘭𝘜𝘴𝘦𝘳𝘴() { 𝘳𝘦𝘵𝘶𝘳𝘯 𝘶𝘴𝘦𝘳𝘚𝘦𝘳𝘷𝘪𝘤𝘦.𝘨𝘦𝘵𝘈𝘭𝘭𝘜𝘴𝘦𝘳𝘴(); } } 🔥 𝟮. @𝗦𝗲𝗿𝘃𝗶𝗰𝗲 👉 Marks the business logic layer ✔ Keeps controllers clean ✔ Helps follow layered architecture @𝘚𝘦𝘳𝘷𝘪𝘤𝘦 𝘱𝘶𝘣𝘭𝘪𝘤 𝘤𝘭𝘢𝘴𝘴 𝘜𝘴𝘦𝘳𝘚𝘦𝘳𝘷𝘪𝘤𝘦 { 𝘱𝘶𝘣𝘭𝘪𝘤 𝘓𝘪𝘴𝘵<𝘜𝘴𝘦𝘳> 𝘨𝘦𝘵𝘈𝘭𝘭𝘜𝘴𝘦𝘳𝘴() { 𝘳𝘦𝘵𝘶𝘳𝘯 𝘶𝘴𝘦𝘳𝘙𝘦𝘱𝘰𝘴𝘪𝘵𝘰𝘳𝘺.𝘧𝘪𝘯𝘥𝘈𝘭𝘭(); } } 🔥 𝟯. @𝗥𝗲𝗽𝗼𝘀𝗶𝘁𝗼𝗿𝘆 👉 Used for data access layer ✔ Handles DB operations ✔ Provides automatic exception translation @𝘙𝘦𝘱𝘰𝘴𝘪𝘵𝘰𝘳𝘺 𝘱𝘶𝘣𝘭𝘪𝘤 𝘪𝘯𝘵𝘦𝘳𝘧𝘢𝘤𝘦 𝘜𝘴𝘦𝘳𝘙𝘦𝘱𝘰𝘴𝘪𝘵𝘰𝘳𝘺 𝘦𝘹𝘵𝘦𝘯𝘥𝘴 𝘑𝘱𝘢𝘙𝘦𝘱𝘰𝘴𝘪𝘵𝘰𝘳𝘺<𝘜𝘴𝘦𝘳, 𝘓𝘰𝘯𝘨> { } 🔥 𝟰. @𝗔𝘂𝘁𝗼𝘄𝗶𝗿𝗲𝗱 👉 Enables dependency injection ✔ Spring automatically injects required beans ✔ Reduces manual object creation @𝘈𝘶𝘵𝘰𝘸𝘪𝘳𝘦𝘥 𝘱𝘳𝘪𝘷𝘢𝘵𝘦 𝘜𝘴𝘦𝘳𝘚𝘦𝘳𝘷𝘪𝘤𝘦 𝘶𝘴𝘦𝘳𝘚𝘦𝘳𝘷𝘪𝘤𝘦; 💡 Pro tip: Prefer constructor injection over field injection 🔥 𝟱. @𝗧𝗿𝗮𝗻𝘀𝗮𝗰𝘁𝗶𝗼𝗻𝗮𝗹 👉 Manages database transactions ✔ Ensures data consistency ✔ Rolls back on failure @𝘛𝘳𝘢𝘯𝘴𝘢𝘤𝘵𝘪𝘰𝘯𝘢𝘭 𝘱𝘶𝘣𝘭𝘪𝘤 𝘷𝘰𝘪𝘥 𝘤𝘳𝘦𝘢𝘵𝘦𝘜𝘴𝘦𝘳(𝘜𝘴𝘦𝘳 𝘶𝘴𝘦𝘳) { 𝘶𝘴𝘦𝘳𝘙𝘦𝘱𝘰𝘴𝘪𝘵𝘰𝘳𝘺.𝘴𝘢𝘷𝘦(𝘶𝘴𝘦𝘳); } 💡 𝗙𝗶𝗻𝗮𝗹 𝗧𝗵𝗼𝘂𝗴𝗵𝘁 “Good developers write working code. Great developers write clean, maintainable, and scalable code.” These 5 annotations are your foundation to become a great Spring Boot developer 🚀 💬 Tell me in comments: Which annotation do you use the most in your projects? #SpringBoot #Java #BackendDevelopment #Microservices #Programming #SoftwareEngineering #Coding #Developers #Tech #SystemDesign #Learning #100DaysOfCode #CodingLife #DeveloperCommunity #JavaDeveloper
To view or add a comment, sign in
-
-
Recently, I stopped “using” Spring Boot and started understanding it. At first, building a REST API felt easy. Controller. Service. Repository. Done. But as the system grew, real engineering problems showed up: Slow endpoints. N+1 queries. Lazy loading errors. Circular dependencies. Bloated service classes. Security confusion with JWT and filter chains. So I stepped back and studied what was actually happening underneath. I read about: • Dependency Injection and Inversion of Control • How JPA and Hibernate generate SQL • Proper transaction boundaries with @Transactional • DTO vs Entity design • Connection pooling and indexing • Clean Architecture principles Then I started applying it. I moved transactions to the service layer. Replaced entity exposure with DTOs. Used fetch joins to fix N+1 queries. Reduced tight coupling by separating responsibilities. Analyzed SQL logs instead of guessing. The biggest lesson: Spring Boot makes it easy to build. But scaling an API forces you to think about design, boundaries, and tradeoffs. Frameworks don’t create clean systems. Engineers do. Still learning. Still refactoring. Still optimizing. #SpringBoot #Java #BackendDevelopment #SoftwareEngineering #CleanArchitecture #SystemDesign #APIDesign #Microservices #Developers
To view or add a comment, sign in
-
-
🧑💻 Understanding Spring Boot MVC Architecture Spring Boot follows the MVC (Model–View–Controller) design pattern to build clean, scalable, and maintainable backend applications. MVC enforces a clear separation of concerns, which is critical for enterprise-grade systems and long-term maintainability. Let’s walk through how Spring Boot MVC works end-to-end 👇 🔹 1️⃣ Client The flow begins when a client—such as a browser, mobile app, Postman, or another service—sends an HTTP request to the backend. 🔹 2️⃣ DispatcherServlet (Front Controller) This is the core of Spring MVC. It: Receives all incoming HTTP requests Identifies the appropriate controller using handler mappings Manages the complete request–response lifecycle Handles JSON ↔ Java object conversion (via HttpMessageConverters) Think of it as the traffic controller of the application. 🔹 3️⃣ Controller Layer Controllers act as the entry point for requests. Their responsibilities: Accept and validate client input Map requests to appropriate endpoints Delegate business logic to the service layer Return responses (JSON or views) ✔ Controllers should remain thin and focused, avoiding business logic. 🔹 4️⃣ Service Layer This is where the real business logic lives. It: Applies business rules and workflows Processes data Communicates with databases or external APIs Keeps controllers clean and reusable ✔ A well-designed service layer improves testability and scalability. 🔹 5️⃣ Model Layer The model represents the application’s data and domain structure. It includes: Entities DTOs (Data Transfer Objects) Domain objects ✔ Proper modeling ensures consistency between business logic and data storage. 🔹 6️⃣ View / Response In modern REST-based Spring Boot applications, the response is typically JSON, returned to the client as an HTTP response rather than a traditional UI view. 🔹 Why MVC Matters in Spring Boot ✔ Clear separation of concerns ✔ Easier testing and debugging ✔ Scalable and maintainable architecture ✔ Industry-standard enterprise design ✔ Cleaner and more readable codebase If you’re building REST APIs, microservices, or enterprise applications, mastering Spring Boot MVC is not optional—it’s essential. #SpringBoot #MVC #Java #BackendDevelopment #SoftwareArchitecture #RESTAPI #CleanCode #Spring
To view or add a comment, sign in
-
-
🧠 Spring Uses Reflection — Isn’t That Slow? Spring MVC invokes controller methods using reflection. That sounds expensive. So why doesn’t it hurt performance? Let’s break it down 👇 ━━━━━━━━━━━━━━━━ 🔹 What Is Reflection? Reflection is a Java feature that allows a program to: • Inspect classes at runtime • Discover methods and fields • Invoke methods dynamically Normally, Java calls methods directly: userService.getUser(); With reflection, Java can call a method without knowing it at compile time. It finds the method at runtime and executes it dynamically. That’s how frameworks like Spring work. ━━━━━━━━━━━━━━━━ 🔹 Why Reflection Is Used Spring doesn’t know your controller methods when it is written. It discovers them when the application starts. Reflection allows Spring to: • Inspect method signatures • Resolve parameters dynamically • Invoke methods generically That’s what makes the framework flexible. ━━━━━━━━━━━━━━━━ 🔹 Is Reflection Slow? Yes — compared to direct method calls. But here’s the reality: • Reflection happens once per request • The cost is tiny compared to: - Network latency - Database calls - JSON serialization In real systems, I/O dominates CPU cost. Reflection is negligible. ━━━━━━━━━━━━━━━━ 🔹 How Spring Optimizes It Spring caches: • Method metadata • Parameter information • Routing mappings It does NOT scan everything on every request. Most heavy work happens at startup. ━━━━━━━━━━━━━━━━ 💡 Takeaway Reflection gives Spring flexibility. I/O determines performance. Framework internals are rarely your bottleneck — database and network usually are. #SpringBoot #Java #JVM #BackendEngineering
To view or add a comment, sign in
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development
Thanks For sharing This 💯