SPRINGMVC Global Exception interception processing @ControllerAdvice annotations @ExceptionHandler

Source: Internet
Author: User

The first step: Dispatcher Front controller of the source code in the default

Private Boolean Throwexceptionifnohandlerfound = False, indicating that if no matching executor is found, no exception is thrown handler not found, so the 404 exception is to be captured if this property is to be changed to True

You can use the configuration of Web. XML Dispatcher This parameter is true for specific configuration parameters, please Baidu, here does not elaborate

Step Two: Create a new class, add @controlleradvice annotations on this class, meaning that the controller enhancement means that the note can be scanned by spring, and then define a method in this class to handle the exception you intercepted, note that

Add the @exceptionhandler annotation to the top of the method, and the value of the annotation is the class bytecode object of the exception you want to intercept.

Step three: Basically all the work is done on the second step, you just have to make sure that your annotations are scanned in the scan package of the spring MVC configuration file, and then write down the specific processing you have for the interception.

Precautions:

Do not configure the MVC configuration file <mvc:default-servlet-hanler/> If there is a configuration that will affect your definition of global MVC an interception effect Oh, basically the test is successful, if you did not succeed in stating that there is no right,

Basically in accordance with the above steps can be, specific configuration problems to solve their own, the main idea is right, the implementation process has encountered problems is inevitable, I can not be all the possible problems listed out to everyone reference, basically

According to the main steps did not do, the details of the small problem will depend on everyone to study, but also exercise their ability to solve problems, and the knowledge to deepen the impression of understanding, refueling OH

SPRINGMVC Global Exception interception processing @ControllerAdvice annotations @ExceptionHandler

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.