In the development of JavaEE project, it is inevitable to encounter all kinds of predictable and unpredictable exceptions to be dealt with, no matter in the process of the underlying database operation, or the process of the business layer, or the process of the control layer. Each process deals with the exception separately, the system’s code coupling degree is high, the workload is large and not unified, and the workload of maintenance is also large.

SpringMvc provides support for exception handling. Through the global exception handling mechanism provided by SpringMvc, all types of exception handling can be decouple from each processing process, which not only ensures the single function of related processing process, but also realizes the unified processing and maintenance of exception information.

There are three ways that SpringMVC handles global exceptions

  • Using the Spring MVC provides simple exception handler SimpleMappingExceptionResolver;

  • Implementation of Spring’s exception handling interface HandlerExceptionResolver custom exception handler;

  • Use the @ExceptionHandler annotation for exception handling;

A case in field

Global exception Handling Method 1

Configuration SimpleMappingExceptionResolver object

<bean class="org.springframework.web.servlet.handler.SimpleMappingException Resolver">
    <property name="defaultErrorView" value="error"></property>
    <property name="exceptionAttribute" value="ex"></property>
    <property name="exceptionMappings">
        <props>
            <prop  key="com.xxx.exception.BusinessException">error1</prop>
            <prop  key="com.xxx.exception.ParamsException">error2</prop>
        </props>
    </property>
</bean>
Copy the code

Using SimpleMappingExceptionResolver exception handling, integrated with simple, good scalability, and the advantages of no invasive to existing code, but this method can only obtain the abnormal information, if abnormal, to obtain the data in addition to the abnormal situation do not apply.

Global exception handling method 2

Implement the HandlerExceptionResolver interface

@Component
public class GlobalException implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
            Map<String,Object> map=new HashMap<String, Object>();
            map.put("ex", ex);
            ModelAndView mv=null;
            if(ex instanceof ParamsException){
            	return new ModelAndView("error_param", map);
            }
            if(ex instanceof BusinessException){
            	return new ModelAndView("error_business", map);
            }
        return new ModelAndView("error", map); }}Copy the code

Using the exception processor that implements HandlerExceptionResolver interface to handle exceptions has the advantages of simple integration, good expansibility and no intrusion on existing codes. At the same time, the object causing the exception can be obtained during exception processing, which is conducive to providing more detailed exception processing information.

Global exception handling method 3

The page processor inherits from BaseController

public class BaseController {
    @ExceptionHandler
    public String exc(HttpServletRequest request,HttpServletResponse response,Exception ex){
        request.setAttribute("ex", ex);
        if(ex instanceof ParamsException){
            return "error_param"; 
        }
        if(ex instanceof BusinessException){
            return "error_business"; 
        }
        return "error"; }}Copy the code

The @ExceptionHandler annotation is used to implement exception handling. It has the advantages of simple integration, good scalability (only the Controller class to be handled needs to inherit from BaseController), and no additional Spring configuration. However, this method is intrusive to the existing code (the existing code needs to be modified so that the related class inherits from the BaseController), and it cannot obtain data other than the exception when handling the exception.

Extends ~ handling of uncaught exceptions

Unchecked, Unchecked Exception is generally ignored because code is Unchecked. Unchecked, Unchecked Exception is Unchecked. If the runtime produces an Unchecked Exception that is Unchecked in code, you may face an embarrassing 404, 500… Wait for the server internal error message. We need a comprehensive and effective exception handling mechanism. Most servers today also support configuring display pages for specific exceptions in web.xml using either (Websphere/Weblogic) or (Tomcat) nodes. Modify the web. XML file and add the following content: java.lang.throwable /500.jsp 500/500.jsp 404/404.jsp