🚀 Day 22/100: Spring Boot From Zero to Production Topic: REST Controllers Why REST APIs Feel Easy in Spring Boot? Spring Boot gives you everything under one roof. No extra setup. No boilerplate. Just annotate and go. @RestController, Two Annotations in One. It's actually a combo of: @Controller → marks the class as an MVC controller @ResponseBody → ensures JSON is returned, not a view/template Without @ResponseBody, Spring would look for an HTML template. With it? Pure JSON. Every time. Structuring Your Endpoints Add @RequestMapping("/api/v1/resource") at the class level for the common base URL Then map individual methods using: @GetMapping → fetch data @PostMapping → create @PutMapping → full update @PatchMapping → partial update @DeleteMapping → delete Each annotation takes an optional path extension like ("/{id}"). 4 Ways to Accept Input 📥 @PathVariable → from the URL → /products/{id} @RequestParam → from query string → /products?name=shoes @RequestBody → from JSON body → deserialized automatically via Jackson @RequestHeader → from HTTP headers → useful for tokens/auth Control Your Response with ResponseEntity Don't just return objects blindly. Wrap them in ResponseEntity<T> to control: Status codes (201 Created, 404 Not Found, 204 No Content) Response headers Response body Handle Errors in One Place 🛡️ No try/catch in every method. Use @RestControllerAdvice to catch exceptions globally and return clean, consistent error responses across your entire API. One class. Full REST API. That's Spring Boot. #Java #SpringBoot #SoftwareDevelopment #100DaysOfCode #Backend #RESTfulAPI
Spring Boot REST Controllers Simplified
More Relevant Posts
-
Day 21. My API crashed with a StackOverflowError. Not because of bad code. Because of a relationship I didn’t think twice about. I had this: @Entity public class User { @OneToMany(mappedBy = "user") private List<Order> orders; } @Entity public class Order { @ManyToOne private User user; } Looks normal. Until you return it as JSON. Here’s what actually happens: → User → Orders → User → Orders → User… → Infinite recursion → StackOverflowError → Your API crashes That’s not a bug. That’s how your model is designed. Hibernate understands it. Jackson doesn’t. So I fixed it. (see implementation below 👇) Now: → DTOs control the response → No circular references → No accidental data explosion The hard truth: → Bidirectional relationships are easy to write → They’re dangerous to expose → You won’t notice until production Returning entities is convenient. Controlling your API response is what makes you a backend developer. Have you ever hit this error? 👇 Drop it below #SpringBoot #Java #Hibernate #BackendDevelopment #JavaDeveloper
To view or add a comment, sign in
-
-
Clean REST API in Spring Boot (Best Practice) 🚀 Here’s a simple structure you should follow 👇 📁 Controller - Handles HTTP requests 📁 Service - Business logic 📁 Repository - Database interaction Example 👇 @RestController @RequestMapping("/users") public class UserController { @Autowired private UserService userService; @GetMapping("/{id}") public User getUser(@PathVariable Long id) { return userService.getUserById(id); } } 💡 Why this matters: ✔ Clean code ✔ Easy testing ✔ Better scalability ⚠️ Avoid: Putting everything inside controller ❌ Structure matters more than code 🔥 Follow for more practical backend tips 🚀 #SpringBoot #Java #CleanArchitecture #Backend
To view or add a comment, sign in
-
In production Spring Boot services, scattered try-catch blocks create inconsistent API behavior. A better approach is centralized handling: ```@RestControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(ResourceNotFoundException.class) public ResponseEntity<ErrorResponse> handleNotFound(ResourceNotFoundException ex) { return ResponseEntity.status(HttpStatus.NOT_FOUND) .body(new ErrorResponse("RESOURCE_NOT_FOUND", ex.getMessage())); } @ExceptionHandler(MethodArgumentNotValidException.class) public ResponseEntity<ErrorResponse> handleValidation(MethodArgumentNotValidException ex) { return ResponseEntity.status(HttpStatus.BAD_REQUEST) .body(new ErrorResponse("VALIDATION_ERROR", "Invalid request payload")); } @ExceptionHandler(Exception.class) public ResponseEntity<ErrorResponse> handleGeneric(Exception ex) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body(new ErrorResponse("INTERNAL_ERROR", "Unexpected error occurred")); } }``` Benefits we observed: - Consistent contract for error payloads - Cleaner controllers/services - Accurate HTTP semantics (400, 404, 409, 500) - Better observability and incident response A strong error model is part of API design, not just exception handling. #SpringBoot #Java #Microservices #API #SoftwareEngineering #Backend
To view or add a comment, sign in
-
⚠️ Hot Reload in Spring Boot Isn’t Magic. It’s ClassLoader Isolation. When you change code and see: 👉 App restarts in seconds 👉 No full JVM restart What’s really happening? 1️⃣ Two ClassLoaders — The Core Idea Spring Boot DevTools splits your app into: Base ClassLoader → dependencies (stable) Restart ClassLoader → your application code (changing) 2️⃣ Why This Matters Dependencies (like Spring, Hibernate): Loaded once Heavy to reload Rarely change Your code: Changes frequently Needs fast reload So only your code is reloaded. 3️⃣ What Happens on Code Change You modify a .class file DevTools detects change (file watcher) Discards Restart ClassLoader Creates a new one Reloads your classes Base ClassLoader stays untouched 4️⃣ Why It’s Faster Than Full Restart Without DevTools: JVM restarts All classes reloaded Framework reinitialized With DevTools: Only your classes reload Dependencies reused Startup drastically reduced 5️⃣ What Actually Gets Restarted? Spring ApplicationContext Beans created again Config reloaded But: 👉 JVM process stays alive 👉 Core libraries remain loaded 6️⃣ Common Gotcha (Very Important) Static state survives in Base ClassLoader. Example: static Map cache = new HashMap(); If loaded by base loader: It won’t reset after reload Leads to weird bugs 7️⃣ Why Some Changes Don’t Reload DevTools won’t reload when: Dependency JAR changes Config outside classpath changes Native resources updated Requires full restart 8️⃣ Comparison With Other Tools Tool Approach DevTools ClassLoader restart JRebel Bytecode rewriting HotSwap Limited JVM replacement DevTools doesn’t “reload classes” It replaces the ClassLoader that loaded them #Java #SpringBoot #JVM #ClassLoader #HotReload #BackendEngineering #SoftwareEngineering #LearnInPublic
To view or add a comment, sign in
-
💡 How many of us REALLY know how "@Transactional" works in Spring Boot? Most developers use "@Transactional" daily… But under the hood, there’s a lot more happening than just "auto rollback on exception". Let’s break it down 👇 🔹 What is "@Transactional"? It’s a declarative way to manage database transactions in Spring. Instead of manually writing commit/rollback logic, Spring handles it for you. --- 🔍 What actually happens behind the scenes? 1️⃣ Spring creates a proxy object around your service class 2️⃣ When a method annotated with "@Transactional" is called → it goes through the proxy 3️⃣ The proxy: - Opens a transaction before method execution - Commits if everything succeeds ✅ - Rolls back if a runtime exception occurs ❌ --- ⚙️ Execution Flow Client → Proxy → Transaction Manager → Target Method → DB --- 🚨 Important Gotchas ❗ Works only on public methods ❗ Self-invocation (method calling another method inside same class) will NOT trigger transaction ❗ By default, only unchecked exceptions trigger rollback ❗ Uses AOP (Aspect-Oriented Programming) --- 🧠 Advanced Concepts ✔ Propagation (REQUIRED, REQUIRES_NEW, etc.) ✔ Isolation Levels (READ_COMMITTED, SERIALIZABLE) ✔ Transaction Manager (PlatformTransactionManager) ✔ Lazy initialization & session handling --- 🔥 Example @Service public class PaymentService { @Transactional public void processPayment() { debitAccount(); creditAccount(); // If credit fails → debit will rollback automatically } } --- ✨ Pro Tip Understanding "@Transactional" deeply can save you from: - Data inconsistencies - Hidden bugs - Production failures --- 👉 Next time you use "@Transactional", remember — you're not calling a method… you're triggering a proxy-driven transaction lifecycle! #SpringBoot #Java #BackendDevelopment #Microservices #TechDeepDive #Learning
To view or add a comment, sign in
-
-
🚀 JsonApi4j 1.6.1 is out — and there's a new home for it too! 👉 https://api4.pro/ The headline this time isn't a single feature — it's a refreshed api4.pro: a new landing page, restructured docs with a sidebar navigation, and a contact page. What's new on the site: → Splash landing page with feature highlights and a quick-start code snippet → Docs reorganized into 5 clear sections: Introduction, Getting Started, Framework Internals, Plugins, Advanced → Contact page for questions, feedback, and collaboration A quick recap of what's been added since my previous post about the 1.5.0 release. Also shipped since then: 🔧 Spec compliance — Added the missing POST / DELETE operations for To-Many relationships. Before, only full replacement (PATCH) was supported; now you can add or remove specific relationship members per the JSON:API spec. 📋 Startup state report & integrity checks — At boot, the framework prints a human-readable summary of registered resources, relationships, operations, and plugins, alongside misconfiguration warnings from new cross-checks across the Domain and Operations registries. Catch issues before the first request, and see exactly what's wired up. 📡 Custom response headers and status — Operations can now propagate arbitrary HTTP headers from downstream responses to the client, and override the response status code per-operation. 🪵 Logging strategy refactor — Most messages moved from INFO to DEBUG, with new log statements added across the processing pipeline. Cleaner production logs by default, richer detail when you need to debug. 🐛 Bugfixes — Plugin auto-configuration in the Spring module, and a property mix-up in the Quarkus module. --- JsonApi4j (https://lnkd.in/ezei7Usv) is an open-source framework for building APIs aligned with the JSON:API specification, with a strong focus on developer productivity and clean architecture. If you've been curious about the framework, this is a great time to take a look — the new docs make it much easier to get started. Feedback and contributions are always welcome! 🙌 #java #jsonapi #opensource #api #documentation
To view or add a comment, sign in
-
🚀 Mastering Spring Boot Annotations (Complete Cheat Sheet for Developers) If you're working with Spring Boot, annotations are your superpower 💡 They reduce boilerplate, improve readability, and let you focus on building features instead of wiring code. Here’s a structured breakdown 👇 🔹 Core Annotations @SpringBootApplication → Entry point of your app @Configuration → Defines configuration class @Bean → Creates Spring-managed objects @Component → Generic Spring bean 🔹 Dependency Injection @Autowired → Inject dependencies automatically @Qualifier → Resolve multiple bean conflicts @Inject → Standard alternative to @Autowired 🔹 Stereotype Layers (Clean Architecture) @Controller → Handles HTTP requests @Service → Business logic layer @Repository → Data access layer 🔹 Spring MVC (Web Layer) @RequestMapping → Maps HTTP requests @GetMapping / @PostMapping → Specific HTTP methods @RequestBody → JSON → Java object @PathVariable → Extract values from URL @RequestParam → Read query parameters 🔹 Spring Data JPA (Database Layer) @Entity → Represents table @Id → Primary key @OneToMany → Relationship mapping @Transactional → Ensures data consistency 🔹 Configuration & Profiles @ConfigurationProperties → Bind config to class @Profile → Environment-specific beans @Conditional* → Load beans conditionally 🔹 Async & Scheduling @Async → Run tasks in background @Scheduled → Run tasks at intervals @EnableScheduling → Enable scheduler 🔹 Validation & Caching @Valid → Trigger validation @NotNull → Field validation @Cacheable → Store results in cache @CacheEvict → Clear cache 🔹 Testing & Security @SpringBootTest → Full app testing @MockBean → Mock dependencies @PreAuthorize → Role-based access control 📌 What stands out: Spring Boot hides complexity but still gives you full control when you need it. 💭 Think of annotations as: "Instructions to Spring — you define WHAT, it handles HOW." #SpringBoot #Java #BackendDevelopment #SoftwareEngineering #Coding #Developers #TechLearning
To view or add a comment, sign in
-
Your Spring Boot API is slow. Not because of bad code. Because of invisible garbage. A simple request like GET /api/users/123 looks harmless. But Spring Boot creates a storm of temporary objects you never see: → Tomcat builds request/response objects → Spring Security creates SecurityContext → Filters wrap everything in decorators → Hibernate creates lazy-load proxies → Dirty checking snapshots entity state → Jackson builds serialization objects → @Transactional wraps services in proxies You wrote 15 lines. The framework created thousands of objects. At 100 RPS? Fine. At 1,000 RPS? Disaster. Young Gen fills every second GC runs constantly p99 latency spikes CPU burns on cleanup, not business logic No crash. No exception. Just mysteriously slow performance. ━━━━━━━━━━━━━━━━ How I reduce object creation: Return DTOs, not entities Use @Transactional(readOnly = true) for reads Use log placeholders: log.info("User {}", id) Prefer primitives (int vs Integer) in hot paths Avoid streams/maps in performance-critical loops Tune JVM Young Gen size Profile with Java Flight Recorder ━━━━━━━━━━━━━━━━ The real performance truth: The fastest code isn't code that executes the fastest. It's code that creates the least garbage. Master this, and your APIs will scale. What's your go-to optimization for high-traffic Spring Boot apps? #SpringBoot #Java #Microservices #BackendDevelopment #SoftwareArchitecture
To view or add a comment, sign in
-
-
🚀 Improving API Performance using Multi-Threading in Spring Boot In today’s fast-paced systems, API latency directly impacts user experience and business revenue. I recently built a small project to understand how synchronous vs asynchronous processing affects performance in a microservices-like setup. 🔍 Use Case A service needs to fetch: * Product details * Price * Inventory from different sources (simulated as separate services). --- ❌ Problem with Synchronous Approach All calls run in a single thread: * Product → Price → Inventory * Each call waits for the previous one * Total time ≈ 6+ seconds (due to delays) --- ✅ Solution: Asynchronous with Multi-Threading Using Java’s CompletableFuture, we run all calls in parallel: * Product → Thread 1 * Price → Thread 2 * Inventory → Thread 3 ⏱ Result: Total time reduced to ~2 seconds --- 💡 Key Learning * Don’t block a single thread for independent tasks * Use parallel execution for IO-bound operations * `CompletableFuture` is a simple and powerful way to achieve concurrency in Spring Boot --- 📊 Performance Comparison * Sync: ~6.7s * Async: ~2.1s --- 📌 Takeaway Whenever your API aggregates data from multiple services, go async to reduce latency and improve scalability --- I’ll be sharing: 👉 Code breakdown 👉 Interview questions from this concept 👉 Real-world improvements (thread pools, error handling) Stay tuned 🔥 #Java #SpringBoot #BackendDevelopment #Microservices #Multithreading #Performance #APIDesign
To view or add a comment, sign in
-
📘 Part 2: Deep Dive — API Performance Optimization using Multithreading In my previous post, I showed how switching from synchronous → asynchronous execution reduced API response time from ~6.7s to ~2.1s 🚀 Today, I’m breaking that down into a structured learning module you can actually revise and apply. 🧠 Problem Recap You have an API that aggregates data from multiple sources: Product Service Price Service Inventory Service ❌ In a synchronous flow, everything runs in a single thread: → Total Time = T1 + T2 + T3 → Result: Slow & blocking ⚡ Solution: Multithreading with CompletableFuture Instead of waiting for each call: ✅ Run them in parallel threads → Total Time = max(T1, T2, T3) 🔑 Core Implementation Idea CompletableFuture<Product> productFuture = CompletableFuture.supplyAsync(() -> productService.findById(id)); CompletableFuture.allOf(productFuture, priceFuture, inventoryFuture).join(); ✔ Parallel execution ✔ Non-blocking ✔ Faster response 📊 Performance Comparison Approach Time Taken Synchronous ~6.75 sec Asynchronous ~2.1 sec 🏗 Architecture Used Controller → Facade → Service → Repository → DB 👉 Facade layer acts as an orchestrator (important design pattern) ⚠️ When to Use This? Use async when: ✔ Multiple independent API calls ✔ IO-bound operations ✔ Aggregation APIs Avoid when: ❌ Tasks depend on each other ❌ CPU-heavy processing 💡 Real-World Best Practices Use custom thread pools (don’t rely on defaults) Handle failures with .exceptionally() Add timeouts for external calls Monitor using metrics (Micrometer / Prometheus) 🎯 Key Takeaway 👉 “Don’t block a thread when you don’t have to.” Parallel execution is one of the simplest yet most powerful optimizations in backend systems. Next post I’ll cover: 🔥 Common mistakes with CompletableFuture 🔥 How to avoid thread pool issues 🔥 Production-grade improvements Follow along if you’re into backend performance 👇 #Java #SpringBoot #Multithreading #BackendEngineering #Microservices #PerformanceOptimization
Software developer| Java (8,17,21)|| Spring boot |Jira |Rest API |Tomcat |Hibernate MySQL Postgres SQL |Spring Security | JSON |Html | Microservices | kafka
🚀 Improving API Performance using Multi-Threading in Spring Boot In today’s fast-paced systems, API latency directly impacts user experience and business revenue. I recently built a small project to understand how synchronous vs asynchronous processing affects performance in a microservices-like setup. 🔍 Use Case A service needs to fetch: * Product details * Price * Inventory from different sources (simulated as separate services). --- ❌ Problem with Synchronous Approach All calls run in a single thread: * Product → Price → Inventory * Each call waits for the previous one * Total time ≈ 6+ seconds (due to delays) --- ✅ Solution: Asynchronous with Multi-Threading Using Java’s CompletableFuture, we run all calls in parallel: * Product → Thread 1 * Price → Thread 2 * Inventory → Thread 3 ⏱ Result: Total time reduced to ~2 seconds --- 💡 Key Learning * Don’t block a single thread for independent tasks * Use parallel execution for IO-bound operations * `CompletableFuture` is a simple and powerful way to achieve concurrency in Spring Boot --- 📊 Performance Comparison * Sync: ~6.7s * Async: ~2.1s --- 📌 Takeaway Whenever your API aggregates data from multiple services, go async to reduce latency and improve scalability --- I’ll be sharing: 👉 Code breakdown 👉 Interview questions from this concept 👉 Real-world improvements (thread pools, error handling) Stay tuned 🔥 #Java #SpringBoot #BackendDevelopment #Microservices #Multithreading #Performance #APIDesign
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