Client is sending the request. Getting response. If everything is okay. Then we are good. But if something fails to work like It supposed to, Then this is part of exception that it throws. What you want to send as a responds when there is an exception?
By default, Spring Boot is imparting a fallback error-handling page, and error-handling response in case of REST requests. Particularly, Spring Boot appears for a mapping for the /error endpoint during the start-up. This mapping depends on what is set on a ViewResolver class. When no legitimate mappings can be found, Spring Boot mechanically configures a default fallback error page. This so-called Whitelabel Error Page is nothing more than a white HTML page containing the HTTP reputation code and a uncertain error message.
This is what the Whitelabel HTML page looks like in your browser:
We need some error code or error message that is sent as JSON and not some random html page that tomcat server throws up right…
There are couple of ways ,we can approach error handling in REST API.
1. We can create an exception and we can throw it if there is no id.
2. Customize Error Response
1. Custom Exception:
When you use Spring boot with Rest API then, We have to create a custom exception class like EmployeeNotFoundException.
Let’s first create EmployeeNotFoundException class. and this I want to be as an RuntimeException so iam going to extends RuntimeException, then we have to add Serial id. Because RuntimeException have serial id. Then we create constructor. This constructor takes the String and calls the parent. So actually this exception is thrown when the data we are looking for is not found return employeeRepository.findEmployeeById(id)
Go to POSTMAN. I have addaed 2 employees. Now i wanted to check employee ID which is not existing. When we hit the URL, This is the below response we get when we get for non existing employee.
I don’t want to send status code 500. I want to send 404. For this You can specify the Response Status for a specific exception along with the definition of the Exception '@ResponseStatus' annotation.
Now go to POSTMAN. Hit the send button.You will get below responds.
You got desired result right. This is the way we have to CUstom our Exception in Spring Boot Rest API.
Now let's see Second approarch.
2. Customize error Response:
I don’t want to simply send 404. I want to send specific error message. For that we have to create error response bean class that has date timestamp, string message, string detail and Allargconstructor.
Then create CustomizedResponseEntityExceptionHandler. This class extends ResponseEntityExceptionHandler. In order to get ErrorDetails to return the error response, we have to define a @ControllerAdvice annotation to this class.
• @ExceptionHandler() indicates that this method would handle exceptions of the specific type.
• ResponseEntity(errorDetails, HttpStatus.NOT_FOUND) - Create an error response object and return it with a specific Http Status
Now Hit the send button in POSTMAN. We will get below response.
Related video for this Post: