Inherited the Simplemappingexceptionresolver.
Put code on it.
/*** Global handling of Controller exceptions * distinguishes exception handling for ordinary requests and AJAX requests, normal requests are returned to the configured ErrorCode page, or returned to the specified page *@author * */ Public classCustomexceptionextendsSimplemappingexceptionresolver {Private Final transientLogger Logger =Loggerfactory.getlogger (GetClass ()); @OverrideprotectedModelandview doresolveexception (httpservletrequest request, httpservletresponse response, Object handler, Exception ex) {String viewName=Determineviewname (ex, request); if(ViewName! =NULL) {//JSP format returned//add normal commit to return to your page ErrorPageString errorpage = string.valueof (Request.getattribute ("ErrorPage")); //go back to your page if(Stringutils.isnotblank (errorpage)) {ViewName=ErrorPage; } if(! (Request.getheader ("accept"). IndexOf ("Application/json") >-1 | |(Request. GetHeader ("X-requested-with")! =NULL&&request. GetHeader ("X-requested-with"). IndexOf ("XMLHttpRequest") >-1))) { //if it is not an asynchronous request//Apply HTTP Status code for error views, if specified. //Only apply it if we ' re processing a top-level request.Integer StatusCode =Determinestatuscode (Request, viewName); if(StatusCode! =NULL) {applystatuscodeifpossible (Request, response, StatusCode); } returnGetmodelandview (ViewName, ex, request); } Else{//JSON format returns Try{Map<string, object> jsonmap =NewHashmap<string, object>(); //return is an errorJsonmap.put (Basecontroller.ajax_result,false); Jsonmap.put (Basecontroller.result_message, Ex.getmessage ()); Response.setcontenttype ("Text/html;charset=utf-8"); PrintWriter writer=Response.getwriter (); Writer.write (json.tojsonstring (Jsonmap)); Writer.close (); } Catch(Exception e) {logger.error ("Doresolveexception", "System exception!" ", E); } return NULL; } } Else { return NULL; } }}
Spring.xml Configuration
<Beanclass= "Cn.tomcat.quickstart.exception.CustomException"> <!--defines the default exception handling page that is used when registering the exception type - < Propertyname= "Defaulterrorview"value= "Error"></ Property> <!--defines the name of the variable that the exception handling page uses to get exception information, by default named Exception - < Propertyname= "Exceptionattribute"value= "Ex"></ Property> <!--defines exceptions that require special handling, with the class name or full pathname as key, and the name of the page as the value - < Propertyname= "Exceptionmappings"> <Props> <propKey= "IOException">Error/ioexp</prop> <propKey= "Java.sql.SQLException">Error/sqlexp</prop> </Props> </ Property> </Bean>
SPRINGMVC Global Exception exception handling Simplemappingexceptionresolver