Detail three approaches to Spring global exception handling

  • 2020-06-19 10:22:11
  • OfStack

In the development of J2EE project, it is inevitable to encounter all kinds of predictable and unpredictable exceptions to be handled, no matter for the underlying database operation process, business layer processing process, or control layer processing process. Each process handles exceptions separately, the code coupling degree of the system is high, the workload is large and difficult to unify 1, the workload of maintenance is also large. Then, can all types of exception handling be decoupled from each processing process, so as to ensure that the function of related processing process is more single 1 and realize unified processing and maintenance of exception information? The answer is yes. The resolution and implementation of exception handling using Spring MVC Suite 1 is described below

Use SimpleMappingExceptionResolver provided by Spring MVC Implement Spring's exception handling interface HandlerExceptionResolver to customize its own exception handler Implement exception handling using the @ExceptionHandler annotation

(1) SimpleMappingExceptionResolver

This method has the advantages of simple integration, good extensibility and no invasion to the existing code. However, this method can only obtain exception information. In case of exception, it is not applicable to the case that data other than exception should be obtained.


@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.balbala.mvc.web"})
public class WebMVCConfig extends WebMvcConfigurerAdapter{
 @Bean
  public SimpleMappingExceptionResolver simpleMappingExceptionResolver()
  {
    SimpleMappingExceptionResolver b = new SimpleMappingExceptionResolver();
    Properties mappings = new Properties();
    mappings.put("org.springframework.web.servlet.PageNotFound", "page-404");
    mappings.put("org.springframework.dao.DataAccessException", "data-access");
    mappings.put("org.springframework.transaction.TransactionException", "transaction-Failure");
    b.setExceptionMappings(mappings);
    return b;
  }
}

(2) HandlerExceptionResolver

Compared to the first,HandlerExceptionResolver accurately displays the defined exception handling pages, achieving the goal of unified exception handling

1. Define 1 class to implement HandlerExceptionResolver interface, post 1 of your previous code this time


package com.athena.common.handler;
import com.athena.common.constants.ResponseCode;
import com.athena.common.exception.AthenaException;
import com.athena.common.http.RspMsg;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/** 
 * Created by sam on 15/4/14. 
 */
public class GlobalHandlerExceptionResolver implements HandlerExceptionResolver {  
 private static final Logger LOG = LoggerFactory.getLogger(GlobalHandlerExceptionResolver.class);         
  /**   
  *  Handle all the exception information here    
  */  
  @Override  
  public ModelAndView resolveException(HttpServletRequest req,                     HttpServletResponse resp, Object o, Exception ex) {  
    ex.printStackTrace();   
    if (ex instanceof AthenaException) {  
      //AthenaException for 1 A custom exception 
      ex.printStackTrace();     
      printWrite(ex.toString(), resp);   
      return new ModelAndView(); 
    }  
    //RspMsg for 1 Three custom classes that handle exception information  
    //ResponseCode for 1 A custom error code interface 
    RspMsg unknownException = null;   
    if (ex instanceof NullPointerException) {    
      unknownException = new RspMsg(ResponseCode.CODE_UNKNOWN, " Business null exception ", null);
    } else {     
      unknownException = new RspMsg(ResponseCode.CODE_UNKNOWN, ex.getMessage(), null);    }   
      printWrite(unknownException.toString(), resp);  
      return new ModelAndView();  
  } 

  /**   
  *  Add an error message to response In the    
  *   
  * @param msg   
  * @param response   
  * @throws IOException   
  */  
  public static void printWrite(String msg, HttpServletResponse response) {   
     try {      
       PrintWriter pw = response.getWriter();    
       pw.write(msg);    
       pw.flush();    
       pw.close();   
     } catch (Exception e) {     
       e.printStackTrace();   
     }  
  }
}

2. Add spring configuration, only the relevant parts are posted here


import com.athena.common.handler.GlobalHandlerExceptionResolver;
import org.springframework.context.annotation.Bean;
import com.athena.common.handler.GlobalHandlerExceptionResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/** 
 * Created by sam on 15/4/14. 
 */
public class WebSpringMvcConfig extends WebMvcConfigurerAdapter {

  @Bean
  public GlobalHandlerExceptionResolver globalHandlerExceptionResolver() {
   return new GlobalHandlerExceptionResolver();
  }
}

(3)@ExceptionHandler

This is how my current project works, and only the relevant parts are posted here

1. First, define a parent class and implement some basic methods


package com.balabala.poet.base.spring;
import com.google.common.base.Throwables;
import com.raiyee.poet.base.exception.MessageException;
import com.raiyee.poet.base.utils.Ajax;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

public class BaseGlobalExceptionHandler {  
   protected static final Logger logger = null;  
   protected static final String DEFAULT_ERROR_MESSAGE = " The system is busy, please try again later "; 

   protected ModelAndView handleError(HttpServletRequest req, HttpServletResponse rsp, Exception e, String viewName, HttpStatus status) throws Exception {  
     if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null)    
     throw e;   
     String errorMsg = e instanceof MessageException ? e.getMessage() : DEFAULT_ERROR_MESSAGE;    
     String errorStack = Throwables.getStackTraceAsString(e);  

     getLogger().error("Request: {} raised {}", req.getRequestURI(), errorStack);    
     if (Ajax.isAjax(req)) {    
       return handleAjaxError(rsp, errorMsg, status);  
     }    
     return handleViewError(req.getRequestURL().toString(), errorStack, errorMsg, viewName); 
   }  

   protected ModelAndView handleViewError(String url, String errorStack, String errorMessage, String viewName) {    
     ModelAndView mav = new ModelAndView();    
     mav.addObject("exception", errorStack);    
     mav.addObject("url", url);   
     mav.addObject("message", errorMessage); 
     mav.addObject("timestamp", new Date());    
     mav.setViewName(viewName);  
     return mav;  
    }  

   protected ModelAndView handleAjaxError(HttpServletResponse rsp, String errorMessage, HttpStatus status) throws IOException {    
      rsp.setCharacterEncoding("UTF-8");    
      rsp.setStatus(status.value());   
      PrintWriter writer = rsp.getWriter();
      writer.write(errorMessage);    
      writer.flush();    
      return null;  
   }  

   public Logger getLogger() {    
      return LoggerFactory.getLogger(BaseGlobalExceptionHandler.class);
   } 
}

2. Implement the appropriate handling for the exception you want to catch


package com.balabala.poet.base.spring;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.NoHandlerFoundException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@ControllerAdvice
public class GlobalExceptionHandler extends BaseGlobalExceptionHandler {  

   // Such as 404 Is caught by this method 
   @ExceptionHandler(NoHandlerFoundException.class)  
   @ResponseStatus(HttpStatus.NOT_FOUND)  
    public ModelAndView handle404Error(HttpServletRequest req, HttpServletResponse rsp, Exception e) throws Exception {  
       return handleError(req, rsp, e, "error-front", HttpStatus.NOT_FOUND);  
    }  

   //500 The exception will be caught by this method 
   @ExceptionHandler(Exception.class)   
   @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) 
   public ModelAndView handleError(HttpServletRequest req, HttpServletResponse rsp, Exception e) throws Exception { 
       return handleError(req, rsp, e, "error-front", HttpStatus.INTERNAL_SERVER_ERROR); 
   }  

   //TODO  You can rewrite it 1 Methods to catch your custom exceptions 
   //TRY NOW!!!

   @Override  
   public Logger getLogger() {   
      return LoggerFactory.getLogger(GlobalExceptionHandler.class);  
   }

 }


Related articles: