الإنشاء: POST والرمز 201 وLocation
في هذا الدرس 4 موضوعات
هذا الدرس مخطط له. يعرض المخطط أدناه الموضوعات التي سيغطيها، ولم يُنشر الدرس الكامل بعد.
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
في هذا الدرس
implement creation the standard way.
الموضوعات المخطط لها
POST /categorieswith a JSON body creates a new item. The server picks the ID.- Bind the body with
@RequestBodyinto a request DTO (a plain record). - Return
201 Createdand aLocationheader with the URL of the new item. - Return the created representation in the body so the client sees the ID.
- Invalid input gives
400; validation is covered in JNR-P2 and applied here.
تفاصيل المخطط المبدئي
عرض عملي
@PostMapping
ResponseEntity<CategoryResponse> create(@RequestBody CreateCategoryRequest req) {
CategoryResponse c = service.create(req);
URI location = URI.create("/categories/" + c.id());
return ResponseEntity.created(location).body(c);
}اختبر فهمك
Which header holds the new item's URL?
أظهر الإجابة
Location.
التطبيق
طبّق هذا الدرس بمختبر الوحدة وتمارينها واختبارها.
تعليم الدرس كمكتمل اختياري ويمكنك التراجع عنه في أي وقت. يبقى تقدمك محفوظًا في هذا المتصفح.