Now that you can communicate between your controller and your data access layer, you’ve bridged the gap between an end user of your API and the database.
One of the fastest ways to test this functionality is by implementing some GET
endpoints in the controller. The GET
request should be used to retrieve information out of your database, so it makes sense that a GET
request should trigger repository methods like .findAll()
or .findById(Integer id)
.
Continuing the example of the Person
model, our PersonController
may include some @GetMapping
s to accomplish this:
import java.lang.Iterable; import java.util.Optional; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.GetMapping; import com.codecademy.people.entities.Person; import com.codecademy.people.repositories.PersonRepository; @RestController public class PersonController { private final PersonRepository personRepository; public PersonController(final PersonRepository personRepository) { this.personRepository = personRepository; } @GetMapping("/people") public Iterable<Person> getAllPeople() { return this.personRepository.findAll(); } @GetMapping("/people/{id}") public Optional<Person> getPersonById(@PathVariable("id") Integer id) { return this.personRepository.findById(id); } }
In this example, the "/people"
endpoint of this API will fetch all Person
entries from the PersonRepository
using the .findAll()
method offered by the CrudRepository
. It returns an Iterable
, which is just a simplified interface for a collection in Java. Note that the Iterable
has a type parameter, Person
.
You would test this functionality by making a GET
request with curl
, or even navigating to that endpoint in a web browser!
curl localhost:4001/people # [ # { # "id": 1, # "eyeColor": "green", # "name": "Tammy Green", # "age": 31 # }, # { # "id": 2, # "eyeColor": "hazel", # "name": "Rashid Jordan", # "age": 14 # }, # { # "id": 3, # "eyeColor": "brown", # "name": "Aneeqa Kumar", # "age": 23 # } # ]
If, at any point, you see an error like “Failed to connect to localhost port 4001: Connection refused”, your Spring server may be malfunctioning. Fix all errors in your code, and run your code before executing the command
pkill ein
in the terminal to reset the server. Your browser will disconnect and reconnect to Codecademy.
Instructions
In this class, the required dependencies for the controller have already been imported for you. Start by adding a public
method, getAllPlants
, that uses the PlantRepository
to get us an Iterable
of all the Plant
s in the database. There should be a few pre-populated in the database for you.
Don’t add the Spring web annotations just yet!
Now that you’ve leveraged the CrudRepository
method to find all the entries in the database, make it so that a user of your API sees this information when they make a GET
request to the "/plants"
endpoint.
Your application’s first endpoint that interacts with a database is ready! Test it out with a curl
request to localhost:4001/plants
.