Understanding @RequestParam, @QueryParam, @PathParam, and @PathVariable in Spring Boot
1. @RequestParam
- Used to extract query parameters from the request URL.
- Supports optional and default values.
- Commonly used in GET requests.
@RestController
@RequestMapping("/api")
public class UserController {
@GetMapping("/users")
public String getUserByName(@RequestParam(name = "name", required = false, defaultValue = "Guest") String name) {
return "User Name: " + name;
}
}Usage:
GET http://localhost:8080/api/users?name=John
Response:
User Name: John
If the parameter name is missing, it defaults to "Guest".
2. @QueryParam (From JAX-RS, not used in Spring MVC)
- Works similarly to
@RequestParambut is used in JAX-RS (Java EE REST API). - Spring Boot does not use
@QueryParam, instead,@RequestParamshould be used.
Example (JAX-RS, Not Spring Boot):
@Path("/users")
public class UserService {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getUserByName(@QueryParam("name") String name) {
return "User Name: " + name;
}
}3. @PathParam (From JAX-RS, not used in Spring MVC)
- Used in JAX-RS to extract values from the path variable in the URL.
- Spring Boot does not use
@PathParam, instead,@PathVariableshould be used.
Example (JAX-RS, Not Spring Boot):
@Path("/users")
public class UserService {
@GET
@Path("/{id}")
@Produces(MediaType.TEXT_PLAIN)
public String getUserById(@PathParam("id") int id) {
return "User ID: " + id;
}
}4. @PathVariable
- Used in Spring Boot to extract values from the URI path.
- Works with RESTful endpoints.
@RestController
@RequestMapping("/api")
public class UserController {
@GetMapping("/users/{id}")
public String getUserById(@PathVariable("id") int userId) {
return "User ID: " + userId;
}
}Usage:
GET http://localhost:8080/api/users/10
Response:
User ID: 10
Use Case Scenario (Combining @RequestParam and @PathVariable)
Scenario: A user management system where:
- We fetch user details by ID (
@PathVariable). - We apply an optional filter (
@RequestParam) for active/inactive users.
Example Code
@RestController
@RequestMapping("/api")
public class UserController {
@GetMapping("/users/{id}")
public String getUserById(
@PathVariable("id") int userId,
@RequestParam(name = "active", required = false, defaultValue = "true") boolean activeStatus) {
return "User ID: " + userId + ", Active: " + activeStatus;
}
}Usage
GET http://localhost:8080/api/users/10?active=falseResponse:
User ID: 10, Active: false
Summary Table

Key Takeaways
- Use
@RequestParamfor query parameters in Spring Boot. - Use
@PathVariablefor path parameters in Spring Boot. @QueryParamand@PathParamare JAX-RS specific and not used in Spring Boot.
If you found my articles useful, please consider giving it many claps 👏👏👏 and sharing it with your friends and colleagues. Please 🔖 save it for future reference. 🔗 Follow and 🔔subscribe me for more updates






