Blog

Your dream job? Lets Git IT.
Interactive technical interview preparation platform designed for modern developers.

XGitHub

Platform

  • Categories

Resources

  • Blog
  • About the app
  • FAQ
  • Feedback

Legal

  • Privacy Policy
  • Terms of Service

© 2025 LetsGit.IT. All rights reserved.

LetsGit.IT/Categories/Spring
Springmedium

`@RequestParam` vs `@PathVariable` — when do you use which?

Tags
#spring-mvc#requestparam#pathvariable#rest
Back to categoryPractice quiz

Answer

Use `@PathVariable` for identifiers that are part of the resource path (e.g., `/users/{id}`). Use `@RequestParam` for optional filters, pagination, or query options (e.g., `?page=2&sort=name`).

@GetMapping("/users/{id}")
UserDto getUser(@PathVariable String id, @RequestParam(defaultValue = "false") boolean verbose) {
  return service.get(id, verbose);
}

Related questions

Spring
`@RestController` vs `@Controller`: what’s the difference?
#spring#mvc#controller
Spring
`@RestController` vs `@Controller` — what changes?
#restcontroller#controller#spring-mvc
Microservices
REST vs gRPC for service-to-service calls: what are the key trade-offs?
#microservices#grpc
#rest
Architecture
API versioning — when do you version and what are two common strategies?
#api-versioning#backward-compatibility#rest