Spring Boot - Error Handling
Spring Boot Error Handling
Default Exception Handling
Java class that throws an exception.
1 |
|
Spring will check the Accept heather. If text/html is accepted, then show a Whitelabel Error Page. Otherwise, return json with error message
Whitelabel Error PageError message in Json format
1 | { |
To stop Spring from printing stack trace, set server.error.include-stacktrace property to application.properties file. Default is always
1 | server.error.include-stacktrace=never |
You can disable whitelabel page by setting server.error.whitelabel.enabled property to false.
1 | server.error.whitelabel.enabled=false |
ControllerAdvice
ControllerAdvice annotation is a specialization of @Component for classes that declare @ExceptionHandler, @InitBinder, or @ModelAttribute methods to be shared across multiple @Controller classes.
ResponseEntityExceptionHandler defines convenient methods to handle Exception. It is often used with @ControllerAdvice class.
1 |
|
The error handler will handle RuntimeException globally for all @Controller
1 | { |