liveBook · Manning

Path variable

As explained in Spring in Action, Sixth Edition

A path variable is a value embedded in a URL path and bound to a controller method parameter.

A path variable is a value embedded in a URL path and passed to a controller method parameter. In /api/tacos/{id}, {id} is a placeholder for a taco's actual ID. section 7.1.1

Fetching one taco

@GetMapping("/{id}") public Optional<Taco> tacoById(@PathVariable("id") Long id) { return tacoRepo.findById(id); } With a controller base path of /api/tacos, a request to /api/tacos/42 binds 42 to id. The repository then uses that value to find the Taco. section 7.1.1

How Spring in Action, Sixth Edition covers it

How deep it goes
Explains placeholders, parameter binding, repository lookup, Optional results, and explicit 404 handling.
Examples
Uses a taco endpoint that maps /api/tacos/{id} to a repository lookup by Long ID.
What it assumes
Assumes a REST controller with a base path and repository capable of finding entities by ID.
What you can do after
Apply it via Spring REST annotations to retrieve an identified resource and return an appropriate success or not-found response.
How it connects
Connects URL paths with REST controllers, controller method parameters, repositories, Optional, ResponseEntity, and HTTP status codes.

Also explained in

Compare how 2 books explain path variable →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring in Action, Sixth Edition. The section links point to the parts of the book it draws on.