Spring MVC handles exceptions through the Handlerexceptionresolver handler, including Handler mappings, data binding, and exceptions that occur when the target method executes.
Springmvc provided by Handlerexceptionresolver the implementation class
Dispatcherservlet The default assembly Handlerexceptionresolver :
① used a <mvc:annotation-driven/> Configuration :
1. Exceptionhandlerexceptionresolver
1) methods for the definition of @ExceptionHandler annotations in the main process of Handler
2) method priority issues defined @ExceptionHandler annotations: For example, nullpointerexception occurs, but the declared exception has runtimeexception and Exception, This will find the @ExceptionHandler annotation method with the lightest inheritance depth based on the most recent inheritance of the exception, that is, the method that marked the RuntimeException
3) Exceptionhandlermethodresolver Internal if can not find @exceptionhandler annotations, will find @ControllerAdvice in the @ Exceptionhandler Annotation Methods
2. Exception handling _responsestatusexceptionresolver3. Exception Handling _defaulthandlerexceptionresolver
Some special exceptions are handled, such as:
Nosuchrequesthandlingmethodexception,
httprequestmethodnotsupportedexception,
Httpmediatypenotsupportedexception,
Httpmediatypenotacceptableexception and so on.
4. Exception Handling _simplemappingexceptionresolver
If you want to uniformly handle all exceptions, you can use Simplemappingexceptionresolver, which maps the exception class name to the view name, that is, the exception is reported with the corresponding view when an exception occurs
<!--Simple exception Map parser, which maps the exception that the user name has thrown, jumps to the specified view - <BeanID= "Simplemappingexceptionresolver"class= "Org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> < Propertyname= "Exceptionmappings"> <!--The key property is an exception type - <!--Label Body configuration target View - <Props> <propKey= "Com.lamsey.survey.e.usernamealreadyexistexception">Guest/user_regist</prop> </Props> </ Property> </Bean>
SOURCE Analysis:
First, the SPRINGMVC core process is as follows:
Four steps: 1), all requests come in through the Dispatcherservlet 900:dodispatch () method (contains all processes for processing requests and responses) 2), 916;gethandler (processedrequest); The Controller object that can handle this request is obtained according to the current request 3), 923: Find the adapter that can execute each method inside the Controller object 4), the 945:ha.handle adapter calls the target method (using reflection Call Method) 5), 959: Forwarding to page (processing response result) Processdispatchresult exception handling in Processdispatchresult ()
Private void processdispatchresult(httpservletrequest request, httpservletresponse response, Handlerexecutionc Hain Mappedhandler, Modelandview mv, Exception Exception)throwsException {BooleanErrorview =false; if(Exception! =NULL) { if(Exceptioninstanceofmodelandviewdefiningexception) {Logger.debug ("Modelandviewdefiningexception encountered", exception); MV=((modelandviewdefiningexception) exception). Getmodelandview (); } Else{Object handler= (Mappedhandler! =NULL? Mappedhandler.gethandler ():NULL); //Handling Exceptions mv = processhandlerexception (Request, response, handler, exception); Errorview= (mv! =NULL); } } //did the handler return a view to render? if(MV! =NULL&&!mv.wascleared ()) { //This is the normal page rendering processrender (MV, request, response); if(Errorview) {webutils.clearerrorrequestattributes (request); } } Else { if(logger.isdebugenabled ()) {Logger.debug ("Null Modelandview returned to Dispatcherservlet with name '" + getservletname () + "': Assuming Han Dleradapter completed request Handling "); } } if(Webasyncutils.getasyncmanager (Request). Isconcurrenthandlingstarted ()) {//Concurrent handling started during a forward return; } if(Mappedhandler! =NULL) {mappedhandler.triggeraftercompletion (Request, Response,NULL); } }
1) Determine if there is any abnormality
// Handling Exceptions MV = processhandlerexception (request, response, handler, exception);
2) Render the view
// This is the normal page rendering process Render (MV, request, response);
Traversal get exception Handling parser
protectedModelandview processhandlerexception(httpservletrequest request, httpservletresponse response, Ob Ject handler, Exception ex)throwsException {//Check registered Handlerexceptionresolvers ...Modelandview EXMV =NULL; For (Handlerexceptionresolver handlerexceptionresolver:this . handlerexceptionresolvers) { EXMV=handlerexceptionresolver.resolveexception (Request, response, Handler, ex); if(EXMV! =NULL) { Break; } } if(EXMV! =NULL) { if(Exmv.isempty ()) {return NULL; } //We might still need view name translation for a plain error model ... if(!Exmv.hasview ()) {Exmv.setviewname (Getdefaultviewname (request)); } if(logger.isdebugenabled ()) {Logger.debug ("Handler execution resulted in exception-forwarding to resolved error view:" +EXMV, ex); } webutils.exposeerrorrequestattributes (Request, EX, Getservletname ()); returnEXMV; } Throwex; }
SPRINGMVC The default custom page exceptions are as follows;
Try { if(exinstanceofnosuchrequesthandlingmethodexception) { returnHandlenosuchrequesthandlingmethod (nosuchrequesthandlingmethodexception) Ex, request, response, handler); } Else if(exinstanceofhttprequestmethodnotsupportedexception) { returnhandlehttprequestmethodnotsupported (httprequestmethodnotsupportedexception) Ex, request, RESP Onse, Handler); } Else if(exinstanceofhttpmediatypenotsupportedexception) { returnhandlehttpmediatypenotsupported (httpmediatypenotsupportedexception) Ex, request, response, ha Ndler); } Else if(exinstanceofhttpmediatypenotacceptableexception) { returnhandlehttpmediatypenotacceptable (httpmediatypenotacceptableexception) Ex, request, response, handler); } Else if(exinstanceofmissingservletrequestparameterexception) { returnHandlemissingservletrequestparameter (missingservletrequestparameterexception) Ex, request, re Sponse, Handler); } Else if(exinstanceofservletrequestbindingexception) { returnhandleservletrequestbindingexception (servletrequestbindingexception) Ex, request, response, H Andler); } Else if(exinstanceofconversionnotsupportedexception) { returnhandleconversionnotsupported (conversionnotsupportedexception) Ex, request, response, handler); } Else if(exinstanceoftypemismatchexception) { returnHandletypemismatch (typemismatchexception) Ex, request, response, handler); } Else if(exinstanceofhttpmessagenotreadableexception) { returnhandlehttpmessagenotreadable (httpmessagenotreadableexception) Ex, request, response, handler); } Else if(exinstanceofhttpmessagenotwritableexception) { returnhandlehttpmessagenotwritable (httpmessagenotwritableexception) Ex, request, response, handler); } Else if(exinstanceofmethodargumentnotvalidexception) { returnhandlemethodargumentnotvalidexception (methodargumentnotvalidexception) Ex, request, response, handler); } Else if(exinstanceofmissingservletrequestpartexception) { returnhandlemissingservletrequestpartexception (missingservletrequestpartexception) Ex, request, response, handler); } Else if(exinstanceofbindexception) { returnhandlebindexception (bindexception) Ex, request, response, handler); } Else if(exinstanceofnohandlerfoundexception) { returnhandlenohandlerfoundexception (nohandlerfoundexception) Ex, request, response, handler); } } Catch(Exception handlerexception) {Logger.warn ("Handling of [" + Ex.getclass (). GetName () + "] resulted in Exception", handlerexception); } return NULL; }
Springmvc Exception Handling