We have encountered 404,405,500 of these error messages in our Web project, and we need to handle exception information.
Start the application, access a non-existent URL, or modify the processing content to throw an exception directly, such as:
1 |
@RequestMapping ("/hello") |
2 |
Public String Hello () throws Exception { |
3 |
throw new Exception ("error occurred"); |
The default map with the error is in Spring boot, but the error page is not as friendly as it appears to the user.
Unified exception Handling
Although the default error mapping is implemented in Spring boot, in practice, your error page is not friendly enough for users, and we usually need to implement our own exception hints.
Here we begin to learn about unified processing: Creating a global exception-handling class: Defining a uniform exception-handling class by using @controlleradvice instead of defining it individually in each controller. @ExceptionHandler is used to define the type of exception that the function is targeting, and the exception object and request URL are mapped to error.html