Read: single and collection
On This Lesson 4 topics
This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.
In this lesson
read one item or a list, with correct statuses.
Planned topics
GET /categories/{id}binds the ID with@PathVariable.- If the item does not exist, return
404, not200with null. GET /categoriesreturns a list. Query parameters are bound with@RequestParam.- Use
defaultValuefor optional parameters so the code never sees surprises. - Never return a raw entity; return a response DTO.
Draft outline details
Demonstration
@GetMapping("/{id}")
CategoryResponse get(@PathVariable long id) {
return service.find(id)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
}Check your understanding
What status for a missing ID?
Show answer
404.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.