Skip to content
Ameen Abozaid
Planned lessonJNR-019-L02

Read: single and collection

  • Lesson 2 of 8
  • Junior
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, not 200 with null.
  • GET /categories returns a list. Query parameters are bound with @RequestParam.
  • Use defaultValue for optional parameters so the code never sees surprises.
  • Never return a raw entity; return a response DTO.
Draft outline details

Demonstration

java
@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.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Continue to next lesson

Type to search levels, phases, modules and lessons.