@RestControllerAdvice
في هذا الدرس 4 موضوعات
هذا الدرس مخطط له. يعرض المخطط أدناه الموضوعات التي سيغطيها، ولم يُنشر الدرس الكامل بعد.
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
في هذا الدرس
handle exceptions in one place.
الموضوعات المخطط لها
@RestControllerAdviceis a class whose@ExceptionHandlermethods apply to all controllers.- Each handler maps an exception type to a response.
- Remove
try/catchfrom controllers; let exceptions travel up. - Order: specific handlers first; add a catch-all for
Exception. - Validation failures arrive as
MethodArgumentNotValidException.
تفاصيل المخطط المبدئي
عرض عملي
@RestControllerAdvice
class ApiExceptionHandler {
@ExceptionHandler(NotFoundException.class)
ProblemDetail notFound(NotFoundException ex) {
return ProblemDetail.forStatusAndDetail(HttpStatus.NOT_FOUND, ex.getMessage());
}
}اختبر فهمك
Where should the try/catch for not-found live?
أظهر الإجابة
Nowhere in controllers; the advice handles it.
التطبيق
طبّق هذا الدرس بمختبر الوحدة وتمارينها واختبارها.
تعليم الدرس كمكتمل اختياري ويمكنك التراجع عنه في أي وقت. يبقى تقدمك محفوظًا في هذا المتصفح.