Search⌘ K
AI Features

Category API: List all Categories and Edit a Category

Explore how to create backend APIs for listing all categories and editing a category using Spring Boot. Understand validating category IDs, updating data, and enabling CORS to connect the frontend with the backend services.

List all category API

Next, we’ll create an API to list all the categories. Create a function, getCategories, in the controller, which will call the listCategories method in the service.

Java
@GetMapping("/")
public ResponseEntity<List<Category>> getCategories() {
List<Category> body = categoryService.listCategories();
return new ResponseEntity<>(body, HttpStatus.OK);
}
Java
public List<Category> listCategories() {
return categoryRepository.findAll();
}

That’s it. Once we’ve set everything up, creating a new API is ...