Spring MVC exception Handling (Controlleradvice annotations)

Source: Internet
Author: User

@ControllerAdvice, a new annotation provided by spring3.2, can be seen in the name of the general meaning of controller enhancement. Let us first look at the implementation of @controlleradvice:

Java code
    1. @Target (Elementtype.type)
    2. @Retention (Retentionpolicy.runtime)
    3. @Documented
    4. @Component
    5. Public @interface Controlleradvice {
    6. }

Nothing special, the annotations use @component annotations, which can be scanned when we use <context:component-scan> scan.

@ControllerAdvicepublic class Controllerexceptionhanler {private static Logger Logger = Loggerfactory.getlogger ( Controllerexceptionhanler.class); @ExceptionHandler (Value=applicationruntimeexception.class) public Responseentity<string> handleserviceexception (Exception Exception, httpservletrequest request) {return new Responseentity<string> (Exception.getmessage (), httpstatus.bad_request);} @ExceptionHandler (Value=exception.class) @ResponseStatus (value=httpstatus.internal_server_error) public Responseentity<string> handleexception (Exception Exception, httpservletrequest request) {Logger.error ("System exception!") , exception); return new responseentity<string> ("The operation failed, please contact the Administrator!") ", Httpstatus.internal_server_error);}}

  

This can be a global management of the project anomalies, avoid error messages directly displayed to the embarrassment of the page.

Spring MVC exception Handling (controlleradvice annotations)

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.