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.

A GET request is a header only: the verb GET and an address such as http://localhost:8080/categories, /categories?name=Sports or /categories/10

The server retrieves and returns the data.

The server answers with status 200 (success) and a body holding the categories as a JSON array

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.

Insomnia shows 200 OK and a JSON array of categories, starting with Beverages and Condiments

Cheat sheets