GET
Handle a GET request in Spring Boot with @GetMapping, return the categories as JSON, and check it from Insomnia.
Journeyman · Spring Boot · REST API · Updated
Requesting data from a REST API is done through a GET request.

The server retrieves and returns the data.

GET Request - Client
In Insomnia, this is your All categories request from Insomnia: GET:
GET {{ _.base_url }}/api/categories
GET Request - Server
When a client makes a request, the server directs it to a controller. The method that handles the request must be marked with the @GetMapping annotation. Add this method to your CategoriesController:
@GetMapping(path="/api/categories")
public List<Category> getAllCategories ()
{
//use the DAO to get all categories
return categoryDao.getAllCategories();
}
GET Response - Client
Run the app again in IntelliJ, and send All categories from Insomnia. The 404 Not Found from Insomnia: GET is now 200 OK, with every category as a JSON array: the answer you wrote down.
Do it: search the categories by name
The first picture above shows one more kind of GET: /categories?name=Sports, which asks only for the categories with Sports in their name. Spring Boot can hand the controller that name from the address; the real work is the filtering. Write that part as a method of its own.
Category is given at the bottom of the file, with getCategoryId() and getCategoryName().
The method gets two values:
categories: every category, aList<Category>.name: the text from?name=in the address, aString. It'snullwhen the address has no?name=.
The tests use Northwind's eight categories. For example, GET /api/categories?name=con:
| Category | Has con in its name, capitals or not? |
|---|---|
| Beverages | no |
| Condiments | yes |
| Confections | yes |
| Dairy Products | no |
| Grains/Cereals | no |
| Meat/Poultry | no |
| Produce | no |
| Seafood | no |
So search(categories, "con") returns the two categories Condiments and Confections, in that order.
Your job: write the method search(List<Category> categories, String name) so it returns a List<Category>.
- When
nameisnull, return every category. - Otherwise, return the categories whose name contains the text, ignoring capitals:
"SEA"finds Seafood. - Keep them in the order they're in the list.
- When no name matches, return an empty list.
Practiced live on the lesson page: write the Java right there and check it.