واجهات CRUD
في هذه الصفحة 8 موضوعات
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
نظرة عامة
الموضوعات
- Controllers
- أجسام الطلبات (Request bodies)
- معاملات المسار والاستعلام
- الترقيم (Pagination)
- الترتيب (Sorting)
تعلّم أولًا
5 ساعة تدريب (تقدير)
مخرجات التعلم
- Implement create, read, update (PUT and PATCH), and delete endpoints with correct status codes and
Locationheaders. - Bind path variables, query parameters, and request bodies.
- Implement pagination and sorting with limits on page size and allowed sort fields.
- Return
201,204, and404where appropriate.
الدروس
المختبر والتمارين
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)
| ID | Outcome | Task |
|---|---|---|
| JNR-019-EX01 | 1, 4 | Implement POST /suppliers returning 201 with a Location header. |
| JNR-019-EX02 | 2, 4 | Implement GET /suppliers/{id} (path variable) returning 404 when missing, and GET /suppliers?country= (query parameter). |
| JNR-019-EX03 | 1 | Implement PUT /suppliers/{id} (replace) and explain how it differs from PATCH. |
| JNR-019-EX04 | 1 | Implement PATCH /suppliers/{id} so omitted fields stay unchanged. |
| JNR-019-EX05 | 1, 4 | Implement DELETE /suppliers/{id} returning 204, and decide what a missing ID returns. |
| JNR-019-EX06 | 3 | Add page, size, sort to GET /suppliers with max size 50 and allowed sort fields name and country. |
أظهر الحلول
- EX01
ResponseEntity.created(URI.create("/suppliers/" + s.id())).body(s). - EX02
@PathVariable long id; throwResponseStatusException(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(); document404for missing (or204, but be consistent). - EX06
size = Math.min(size, 50);Set.of("name","country").contains(field)else400; addidas tie-breaker.
Outcome 2 is also covered by the lab (path, query, body binding in the catalog endpoints).
الاختبار
Outcomes: O1 create, read, update, delete · O2 binding · O3 pagination and sorting · O4 status codes.
اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.
الجزء العملي
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.
أخطاء شائعة
Unbounded list endpoints; PATCH that nulls missing fields; sorting by arbitrary user-supplied columns.
التقييم
Implement an unseen resource from a spec.
الصلة بالمسار الوظيفي
The daily work of a junior backend engineer.