Spring Boot Parameters
~1 min read
·
Spring Boot
Spring Boot provides a simple way to handle Path Parameters and Query Parameters using @PathVariable and @RequestParam .
Path Parameters ( @PathVariable ) Path parameters are part of the URL (e.g., /users/{id} ). They are mandatory . Used to identify a specific resource .
Example: Get User by ID
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api/users" )
public class UserController {
// ✅ Path Parameter Example
@GetMapping("/{id}" )
public String getUserById (@PathVariable int id ) {
return "User ID: " + id;
}
}
show = true, 3000)" class="absolute top-[10px] right-[10px] inline-flex items-center justify-center gap-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground transition-smooth h-9 rounded-md px-3 cursor-pointer bg-white/80 border border-border">
Test in Browser or Postman
GET http ://localhost:8080/api/users/10
show = true, 3000)" class="absolute top-[10px] right-[10px] inline-flex items-center justify-center gap-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground transition-smooth h-9 rounded-md px-3 cursor-pointer bg-white/80 border border-border">
Response
show = true, 3000)" class="absolute top-[10px] right-[10px] inline-flex items-center justify-center gap-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground transition-smooth h-9 rounded-md px-3 cursor-pointer bg-white/80 border border-border">
Query Parameters ( @RequestParam ) Query parameters are appended after ? in the URL (e.g., /users?name=Alice ). They are optional (can have default values). Used for searching, filtering, sorting .
Example: Search User by Name
@RestController
@RequestMapping("/api/users" )
public class UserController {
// ✅ Query Parameter Example
@GetMapping("/search" )
public String searchUser (@RequestParam String name ) {
return "Searching for user: " + name;
}
}
show = true, 3000)" class="absolute top-[10px] right-[10px] inline-flex items-center justify-center gap-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground transition-smooth h-9 rounded-md px-3 cursor-pointer bg-white/80 border border-border">
Test in Browser or Postman
GET http ://localhost:8080/api/users/search?name=Alice
show = true, 3000)" class="absolute top-[10px] right-[10px] inline-flex items-center justify-center gap-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground transition-smooth h-9 rounded-md px-3 cursor-pointer bg-white/80 border border-border">
Response
Searching for user : Alice
show = true, 3000)" class="absolute top-[10px] right-[10px] inline-flex items-center justify-center gap-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground transition-smooth h-9 rounded-md px-3 cursor-pointer bg-white/80 border border-border">
Multiple Query Parameters You can pass multiple parameters using @RequestParam .
Example: Get Users with Filters
@RestController
@RequestMapping("/api/users" )
public class UserController {
// ✅ Multiple Query Parameters
@GetMapping("/filter" )
public String filterUsers (
@RequestParam(required = false ) String name ,
@RequestParam(required = false ) Integer age ) {
return "Filtering users by Name: " + name + ", Age: " + age;
}
}
show = true, 3000)" class="absolute top-[10px] right-[10px] inline-flex items-center justify-center gap-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground transition-smooth h-9 rounded-md px-3 cursor-pointer bg-white/80 border border-border">
Test with Multiple Parameters
GET http ://localhost:8080/api/users/filter?name=Bob&age=25
show = true, 3000)" class="absolute top-[10px] right-[10px] inline-flex items-center justify-center gap-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground transition-smooth h-9 rounded-md px-3 cursor-pointer bg-white/80 border border-border">
Response
Filtering users by Name : Bob , Age : 25
show = true, 3000)" class="absolute top-[10px] right-[10px] inline-flex items-center justify-center gap-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground transition-smooth h-9 rounded-md px-3 cursor-pointer bg-white/80 border border-border">
Combining Path & Query Parameters You can use both @PathVariable and @RequestParam together .
Set default values " Unknown " if parameter doesn't have city http://localhost:8080/api/user/10/details
Example: Get User Details
@RestController
@RequestMapping("/api/users" )
public class UserController {
// ✅ Path + Query Parameters
@GetMapping("/{id}/details" )
public String getUserDetails (
@PathVariable int id ,
@RequestParam(required = false , defaultValue = "Unknown" ) String city ) {
return "User ID: " + id + ", City: " + city;
}
}
show = true, 3000)" class="absolute top-[10px] right-[10px] inline-flex items-center justify-center gap-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground transition-smooth h-9 rounded-md px-3 cursor-pointer bg-white/80 border border-border">
Test the API
GET http ://localhost:8080/api/users/10/details?city=NewYork
show = true, 3000)" class="absolute top-[10px] right-[10px] inline-flex items-center justify-center gap-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground transition-smooth h-9 rounded-md px-3 cursor-pointer bg-white/80 border border-border">
Response
User ID : 10 , City : NewYork
show = true, 3000)" class="absolute top-[10px] right-[10px] inline-flex items-center justify-center gap-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground transition-smooth h-9 rounded-md px-3 cursor-pointer bg-white/80 border border-border">