Skip to content
Ameen Abozaid
JNR-019

CRUD APIs

  • 8 Lessons
  • 5 h practice (estimate)
On this page 8 topics

Overview

Topics

  • Controllers
  • request bodies
  • path/query parameters
  • pagination
  • sorting

Learn first

5 h of practice (estimate)

Learning outcomes

  1. Implement create, read, update (PUT and PATCH), and delete endpoints with correct status codes and Location headers.
  2. Bind path variables, query parameters, and request bodies.
  3. Implement pagination and sorting with limits on page size and allowed sort fields.
  4. Return 201, 204, and 404 where appropriate.

Lessons

  1. 01Create: POST, 201, Location
  2. 02Read: single and collection
  3. 03Update: PUT vs PATCH
  4. 04Delete
  5. 05Pagination
  6. 06Sorting and its safety limits
  7. 07Demo: full CRUD for categories, live
  8. 08Demo: safe pagination and sorting

Lab and exercises

Lab

JNR-019-LAB1 · Catalog CRUD

Outcomes: 1–4 · Time: ~5 h Implement full product and category CRUD over the in-memory repository, following your JNR-018 design. The product list must be paginated and sortable with a maximum page size and an allow-list of sort fields. Write controller tests (@WebMvcTest) for every endpoint, including 404 and invalid-input cases.

Acceptance: matches the JNR-018 design; page size capped; controller tests cover every endpoint.

Exercises (assessment: implement an unseen resource from a spec)

IDOutcomeTask
JNR-019-EX011, 4Implement POST /suppliers returning 201 with a Location header.
JNR-019-EX022, 4Implement GET /suppliers/{id} (path variable) returning 404 when missing, and GET /suppliers?country= (query parameter).
JNR-019-EX031Implement PUT /suppliers/{id} (replace) and explain how it differs from PATCH.
JNR-019-EX041Implement PATCH /suppliers/{id} so omitted fields stay unchanged.
JNR-019-EX051, 4Implement DELETE /suppliers/{id} returning 204, and decide what a missing ID returns.
JNR-019-EX063Add page, size, sort to GET /suppliers with max size 50 and allowed sort fields name and country.
Show solutions
  • EX01 ResponseEntity.created(URI.create("/suppliers/" + s.id())).body(s).
  • EX02 @PathVariable long id; throw ResponseStatusException(NOT_FOUND) when empty; @RequestParam(required = false) String country.
  • EX03 PUT requires all fields and replaces the item (idempotent); PATCH changes only supplied fields.
  • EX04 Patch DTO with nullable fields; set only those that are not null; test that an unsent field keeps its value.
  • EX05 ResponseEntity.noContent().build(); document 404 for missing (or 204, but be consistent).
  • EX06 size = Math.min(size, 50); Set.of("name","country").contains(field) else 400; add id as tie-breaker.

Outcome 2 is also covered by the lab (path, query, body binding in the catalog endpoints).

Quiz

Outcomes: O1 create, read, update, delete · O2 binding · O3 pagination and sorting · O4 status codes.

A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.

  1. 1After a successful create, the Location header holds:O1
  2. 2PUT differs from PATCH because PUT:O1
  3. 3A PATCH body has only name. The description should:O1
  4. 4Which annotation binds {id} in the path?O2
  5. 5Which binds the JSON body?O2
  6. 6Why cap the page size?O3
  7. 7Sorting should accept:O3
  8. 8A tie-breaker such as id in sorting is used to:O3
  9. 9A successful delete with no body returns:O4
  10. 10GET /categories/999 where none exists returns:O4

Practical part

Six exercises JNR-019-EX01 to EX06 on an unseen resource plus the catalog lab. Pass: at least five exercises and a lab with capped paging and tests for every endpoint.

Common mistakes

Unbounded list endpoints; PATCH that nulls missing fields; sorting by arbitrary user-supplied columns.

Assessment

Implement an unseen resource from a spec.

Career relevance

The daily work of a junior backend engineer.

Type to search levels, phases, modules and lessons.