Hi everyone 👋 📌 Spring Boot Annotation Series Part 21 – @PathVariable @PathVariable is used to extract values from the URL path and bind them to method parameters. It is part of the Spring Framework and widely used in REST APIs built with Spring Boot. 🔹 Why Do We Use @PathVariable? In REST APIs, resources are identified using IDs in the URL. Example: GET /users/101 Here, 101 is part of the URL path. @PathVariable helps us capture that value inside the controller method. 🔹 Basic Example @RestController @RequestMapping("/users") public class UserController { @GetMapping("/{id}") public String getUserById(@PathVariable Long id) { return "User ID is: " + id; } } 👉 If request is: GET http://localhost:8080/users/101 Output: User ID is: 101 🔹 In Simple Words @PathVariable takes dynamic values from the URL and passes them to the controller method. #SpringBoot #Java #RESTAPI #PathVariable #BackendDevelopment #InterviewPreparation
When parameter name is not same as variable? That part is missing ☺
Asmita Sethiya : Great explanation! @PathVariable makes REST endpoint design much cleaner by mapping dynamic URL segments directly to controller parameters. It’s one of those small annotations that really simplifies API development in Spring Boot.