#springboot #exceptions #exceptioninjava #exceptionspringboot #exceptionhandler #springbootexception #globalerrorhandling
HOW TO IMPLEMENT ERROR HANDLING FOR SPRING BOOT REST API
You can download this project on my github(https://github.com/AminTurmudiYusup/r...)
Prerequisite
1. Understand Exception
2. Understand Handler component(try, catch and finaly)
3. Understand Constructing exception in plain java
4. Understand concept how to throw exception and handler by try, catch and finaly block(if not see my previous tutorial = • Understanding Exception in Java )
5. you have spring boot which can consume and produce json
Let's jump right in
Exception Level
1. exception in class using @ExceptionHandler= annotation is only active for that particular class where it is declared.
create class ResourceNotFoundException in package exception which extend RuntimeException
create exception handler inside Employee controller
use exception when user to find employee by id, which data not already exists.
try to run app
try to use exception handle in class DepartementController(not working because, annotation is only active for that particular class where it is declared)
How to define one exception but can use in whole application??
2. A global exception handler using @ExceptionHandler along with @RestControllerAdvice= provides a standard way of handling exceptions throughout the application. In addition, it considerably reduces the amount of code written for exception handling.
Create GlobalErrorHandler class
add annotation @RestControllerAdvice
collect common occurs exception(ResourceNotFoundException, ResourceAlreadyExistException)this exception common occurs in all controller
Create class ResourceAlreadyExistException which extend RuntimeException
create class ResourceNotFoundException which extend RuntimeExcpetion
Create Error Message, to define a description to send to the client(statusCode, message, date)
Create method in GlobalErrorHandler class
try to use ResourceNotFoundException in DepartementController class
try to use ResourceNotFoundException in EmployeeController class
try to run app
with one ResourceNotFoundException can handle exception in whole app.
Succeed, Happy learning and happy sharing !!!
Смотрите видео HOW TO IMPLEMENT GLOBAL ERROR HANDLING FOR SPRING BOOT REST API онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь Programming Tutorial 01 Сентябрь 2022, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 97 раз и оно понравилось like людям.