Spring ReST Exception handling
Handling excpetion in Spring web application and returing them to the client meaningfully is important part of the application design.
First lets see how the error controller in springboot can be customize.
Error Controller
Whenever an error happen, spring directs to /error. The default configuration is vanila. To spice thing a bit, implements the org.springframework.boot.autoconfigure.web.ErrorController
. Now instead of returing valina error message, more detail message is recieved by the client.
Spring ControllerAdvice
Spring ControllerAdvice takes a level more and makes the error handling more easy. From the application exception are throws up at any particular point. ControllerAdvice is the single point of entry to handle all exception and let you define how to hanndle for each or group of exception.
Define an exception class extending the RuntimeException
Create an ErrorMessage Object.
Handle all the exception via ControllerAdvice.
Throw the exception from where relevant and ControllerAdvice will handle it.
For the full project reference visit redis-device-oui