Spring Boot Learning (vi) Unified exception handling for Web applications

Source: Internet
Author: User
Tags exception handling

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");
4 }

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

01

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.