Learn

Rather than get all items, our API users may want to find a specific entity in the database. For this kind of request, we can use CrudRepository methods like .findById(Integer id).

Continuing the example of the Person model, we would add this endpoint to our PersonController:

import java.util.Optional; // Other imports and class scaffolding omitted @GetMapping("/people/{id}") public Optional<Person> getPersonById(@PathVariable("id") Integer id) { return this.personRepository.findById(id); }

The endpoint, "/people/{id}", uses a path parameter so that the user of the API can pass in the id in the URL in their request. The responsibility of the controller class is to extract this id from the request and pass it into the .findById method in the repository.

Note that this method returns an Optional<Person>. This is because the user may pass an ID that does not exist in the database. The Optional type from Java will allow the response to gracefully return null in the response body if the id supplied could not be found in the database.

A request to this endpoint might look like:

# Request for an ID that exists curl localhost:4001/people/3 # { # "id": 3, # "eyeColor": "brown", # "name": "Aneeqa Kumar", # "age": 23 # } # Request for an ID that does not exist curl localhost:4001/people/50 # null

Instructions

1.

Make another GET endpoint that will allow you to fetch a single plant from the database. The endpoint should be available at "/plants/{id}" (using id as a path parameter). Make the method for this endpoint have the name getPlantById, and ensure the name of the path parameter is id.

This method should return an Optional<Plant>, similar to how the getPersonById method returns an Optional<Person>.

2.

Like you did before for the getAllPlants method, use curl to test your getPlantById method. Try it out with any of the Plant IDs that you saw in the response for the previous endpoint you implemented.

Sign up to start coding

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