@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
- @Target (Elementtype.type)
- @Retention (Retentionpolicy.runtime)
- @Documented
- @Component
- Public @interface Controlleradvice {
- }
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)