SpringMVC exception handling mechanism

Processing flow chart

In essence, exceptions are handed to the SpringMVC framework to handle the exceptions of the dao, Service, and controller of the system. All exceptions are thrown upward through throws Exception. Finally, the SpringMVC front-end controller delivers the Exception processor for Exception processing. Springmvc provides a global exception handler (one exception handler per system) for uniform exception handling.

1. Two ways to handle exceptions

1. Using the Spring MVC provides simple exception handler SimpleMappingExceptionResolver 2. Implement Spring’s HandlerExceptionResolver to customize its own exception handler

1. Using the Spring MVC provides simple exception handler SimpleMappingExceptionResolver

Processing logic: Using the Spring MVC provides simple exception handler SimpleMappingExceptionResolver, HandlerExceptionResolver interface processor implementation, Global exception handler needs to implement the interface SimpleMappingExceptionResolver: * * * * is through a simple mapping to determine which view, to deal with the current error message. * * SimpleMappingExceptionResolver: ** Provides the mappings between exceptions and views using the exception type exceptionMappings. When an exception occurs, it provides statusCodes to map the view name returned by the exception to the corresponding HttpServletResponse return code. The default value can also be specified with defaultErrorView and defaultErrorCode. DefaultErrorView means that when no corresponding exception type is found in exceptionMappings, Returns a view defined by TerrorView. Terrorcode means the default return code that is returned when an exception occurs and a mapping is not found in statusCodes which map views to return codes. When using SimpleMappingExceptionResolver, when an exception occurs, SimpleMappingExceptionResolver will put the current exception object in its own attributes exceptionAttribute, ExceptionAttribute When exceptionAttribute is not specified, the default value exception is used

<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <! -- Default error view --> <! Property name= --> <property name="defaultErrorView" value="defaulterror"/ > <! -- Error view for specific type of error --> <property name="exceptionMappings">
            <map>
               <entry key="java.lang.ClassCastException" value="ClassCastException"/>
            </map>
        </property>
    </bean>
Copy the code

2. Implement Spring’s exception handling interface HandlerExceptionResolver to customize its own exception handler

2.1 implementation HandlerExceptionResolver

package com.pjh.Myexception;

import com.sun.org.apache.bcel.internal.generic.MONITORENTER;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyexceptionResolver implements HandlerExceptionResolver {
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        ModelAndView modelAndView = new ModelAndView();
        if (e instanceof MyEcxeption){
          // Handle custom exceptions
     }else{
        // Operations for non-custom exceptions
    }

        returnmodelAndView; }}Copy the code

2.2 Configuration in the Configuration file

 <bean class="com.pjh.Myexception.MyexceptionResolver"/>
Copy the code

The above is for SpringMVC series in the fourth chapter, the main focus for SpringMVC exception handling related resolution, I later will continue to update my learning content, this blog is mainly focused on data structures in the algorithm and Java development, think of my article helpful friend can focus on me, in doubt can comment on DMS, meet is fate, you see high