🚀 Day 7 of Learning Spring Boot — and today was all about writing BETTER code, not just working code. Built two real features today: ✅ Get Product by ID ✅ Search Products by keyword But the biggest lesson wasn't about features — it was about HOW you write them. 🔴 I used to write this: product = service.getProductById(id).get(); if(product != null) { ... } Looks fine, right? WRONG. If the product doesn't exist, .get() throws a NoSuchElementException and the null check never even runs. It's literally dead code. 😅 🟢 The correct way using Optional: service.getProductById(id)  .map(p -> ResponseEntity.ok(p))  .orElseGet(() -> ResponseEntity.notFound().build()); Clean. Safe. No crashes. This is how pros use Optional. 🔑 Golden Rule I learned today: NEVER call .get() directly on an Optional. Always use .map(), .orElse(), or .orElseGet() --- Also built a Search API that matches keyword across name, brand, description & category — all case-insensitive using JPQL LIKE queries. 🔍 And learned a subtle but important Spring MVC rule: 👉 Always define specific routes like /products/search BEFORE dynamic ones like /products/{id} — otherwise Spring treats "search" as an ID. 🤦 Github Repo : https://lnkd.in/g5aeACUU Small mistakes, big lessons. That's what Day 7 looked like. 💪 #SpringBoot #Java #100DaysOfCode #LearningInPublic #BackendDevelopment #JavaDeveloper #SpringFramework

To view or add a comment, sign in

Explore content categories