We are in the Web project, have encountered 404,405,500 such error message, need to handle the exception information,
Launch the app, access a nonexistent URL, or modify the processing 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 error is in Spring boot, but this error page is not very friendly to the user.
Unified exception Handling
Although the default error mapping is implemented in Spring boot, in practice, your error page is not friendly to the user, and we usually need to implement our own exception hints.
Let's start with the unified processing: Create a global exception-handling class: Define 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 the function targets, and finally maps the exception object and the request URL to error.html