In the previous exercise, we discussed the use of @RequestParam to pass data from query parameters to a method. @RequestParam is perfect to use when we want to filter the results or return several resources. However, when we want to return more specific entities we can use the @PathVariable annotation.

@PathVariable maps template variables in the request URI directly to a method’s parameters. For example, we could define a template path


and use the URI


to pass the path variable “28937” to a method’s id parameter. On the server side, we would have an endpoint that looks up books by ID as follows:

@GetMapping("/{id}") public Book isBookAvailable(@PathVariable int id) { return book.findByID(id); }

In the above example, use of the @PathVariable at the method parameter level allows us to take a variable received from the request URI and pass it into a method as a parameter. As a developer, this simple annotation affords us ample opportunities to process data from HTTP requests.

We’ve seen two ways to capture parameters from a request URI. @RequestParam captures the id included in the URI /books?id=28937 and @PathVariable captures the id included in the URI /books/28937 as long as the path includes the {id} variable in books/{id}.



Ingrid likes to keep track of every time one of the super heroes lends their assistance to the public. She’s developed a resource that will report help requests by postal code.

Add the @PathVariable annotation to pass values from the getHeroReportByPostal() method to the endpoint.


Using curl, make a request to the endpoint with an postalCode value of 64672.

Sign up to start coding

By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.
Already have an account?