← All tutorials

GoCareerGo Tutorials

Spring Boot Tutorial

Auto-configuration, REST APIs, JPA, AOP, caching, microservices and Spring Cloud.

@PathVariable vs @RequestParam

Two ways to read data out of a request URL — a path SEGMENT vs a query string parameter.

// GET /api/users/42
@GetMapping("/{id}")
public User getUser(@PathVariable Long id) { ... }

// GET /api/users?page=2&size=20
@GetMapping
public List<User> list(@RequestParam int page, @RequestParam(defaultValue = "10") int size) { ... }
  • @PathVariable — part of the URL path itself, usually an identifier
  • @RequestParam — optional query string key=value pairs, supports defaultValue