Learn

Now that you have completed implementing basic CRUD functionality, it’s natural to ask: What’s next?

Spring Data JPA’s CrudRepository interface is even more powerful than basic findAll and findById queries. We can add custom filter queries to our extension of the CrudRepository simply by adding a properly formatted method name to the interface. You don’t even have to implement the method!

For example, say we have a Person entity with a String eyeColor field, and we wish to query people in the database that have a certain eyeColor. We can simply add a method declaration to the PersonRepository:

package com.codecademy.people.repositories; import java.util.List; import org.springframework.data.repository.CrudRepository; import com.codecademy.people.entities.Person; public interface PersonRepository extends CrudRepository<Person, Integer> { // this declaration is all we need! List<Person> findByEyeColor(String eyeColor); }

Based on how the method is named (findByEyeColor), Spring Data JPA will automatically generate the “implementation” of the method when your application is compiled. This capability is extremely powerful, as it allows developers to create complex queries without even writing the code for them!

To use our newly defined findByEyeColor method, we could implement a new GET /people/search endpoint in the PersonController.

@GetMapping("/people/search") public List<Person> searchPeople( @RequestParam(name = "eyeColor", required = false) String eyeColor ) { if (eyeColor != null) { return this.personRepository.findByEyeColor(eyeColor) } else { return new ArrayList<>(); } }

In the endpoint implementation above, we use query parameters with the @RequestParam annotation. This is the common convention in REST APIs when passing in parameters used for searching or filtering. A request to this endpoint would look like:

curl localhost:4001/people/search?eyeColor=brown # [ # { # "id": 3, # "eyeColor": "brown", # "name": "Aneeqa Kumar", # "age": 23 # } # ]

Take note of these two items; we’ll explain them in the following exercises:

  1. The naming of the method declarations is extremely important here. The rules for the method names are detailed in the Spring documentation here.

  2. We added required = false inside the @RequestParam annotation.

Instructions

1.

Add a custom query method named findByHasFruitTrue() to the PlantRepository interface.

It should yield a list of all plants that have fruit.

2.

Now that you have extended the capability of the PlantRepository, you should use the new functionality in a search endpoint!

Declare a new @GetMapping method in your PlantController called searchPlants, available at "/plants/search". searchPlants should return a List<Plant> and should accept one query parameter:

@RequestParam(name="hasFruit", required=false) Boolean hasFruit

If hasFruit is non-null, and hasFruit is true, you should call the findByHasFruitTrue method.

Otherwise, return an empty list with return new ArrayList<>();.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?