π What Are Parameters in Web Requests?
When building REST APIs or web apps, parameters help you pass data from the client (browser or app) to the server (your Spring Boot backend). In Spring Boot, you can access these parameters using:
- Path Variables
- Request Parameters
- Query Parameters (synonym for Request Params, commonly used term)
They all serve different purposes. Letβs break the Params in Spring Boot down.

π 1. Path Variables (@PathVariable
)
β Use case:
When the value is part of the URL path β typically identifying a specific resource.
π¦ Example URL:
GET /products/42
π Controller Example:
1 2 3 4 5 6 7 8 9 10 11 |
@RestController @RequestMapping("/products") public class ProductController { @GetMapping("/{id}") public String getProduct(@PathVariable int id) { return "Product ID: " + id; } } |
β Output:
Product ID: 42
π Notes:
- Use when identifying a specific resource.
- Clear and readable RESTful URL.
π§Ύ 2. Request Parameters (@RequestParam
)
β Use case:
When the data is passed as key-value pairs after a ?
in the URL.
π¦ Example URL:
GET /products/search?name=keyboard&limit=10
π Controller Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
@RestController @RequestMapping("/products") public class ProductController { @GetMapping("/search") public String searchProducts( @RequestParam String name, @RequestParam(defaultValue = "5") int limit) { return "Searching for: " + name + ", Limit: " + limit; } } |
β Output:
Searching for: keyboard, Limit: 10
π Notes:
- Great for optional or filtering parameters.
- Supports default values and required flags.
π§΅ 3. Query Parameters (Same as Request Params)
β Query params = Request params
They are just different names for the same thing. Some developers use βquery paramsβ for readability.
GET /users?sort=asc&page=2
These are all accessed using @RequestParam
.
π Combining Both: Path + Request Params
You can use both together in the same request:
π¦ Example URL:
GET /orders/55?includeDetails=true
π Controller:
1 2 3 4 5 6 7 8 |
@GetMapping("/orders/{id}") public String getOrder( @PathVariable int id, @RequestParam boolean includeDetails) { return "Order ID: " + id + ", Include details: " + includeDetails; } |
π‘οΈ Making Params Optional
1 2 3 4 5 6 |
@GetMapping("/users") public String listUsers(@RequestParam(required = false) String role) { return role != null ? "Filtered by: " + role : "All users"; } |
Or use a default value:
1 2 3 |
@RequestParam(defaultValue = "user") |
π« Common Mistakes to Avoid
Mistake | Why It’s a Problem |
---|---|
Forgetting @PathVariable or @RequestParam | Spring wonβt map values correctly |
Mismatched variable names | Must match or explicitly specify the param name |
Assuming query param is required | Will throw an error unless marked optional |
Putting sensitive data in URL | Use POST body for credentials, tokens, etc. |
π§ͺ Real-World Examples
Get user by ID:
1 2 3 |
GET /users/100 @PathVariable: 100 |
Filter users by role:
1 2 3 4 5 |
GET /users?role=admin @RequestParam: role=admin |
Pagination:
1 2 3 4 |
GET /users?page=1&size=20 @RequestParam: page=1, size=20 |
β Summary
Type | Annotation | Example | Use Case |
---|---|---|---|
Path Variable | @PathVariable | /users/10 | Resource identification |
Request/Query Param | @RequestParam | /users?role=admin | Filtering, searching, options |
Optional Param | @RequestParam(required = false) | /users | Optional behavior |
Mastering Params in Spring Boot is essential for building clean and effective REST APIs.