美文网首页
spring boot整合全局异常处理

spring boot整合全局异常处理

作者: 盗生一 | 来源:发表于2020-08-19 09:15 被阅读0次
    • 全局异常处理
      @ControllerAdvice 只能处理controller层异常和service抛到controller层未处理的异常
      @ControllerAdvice:生命这个类是异常处理类,交给spring容器管理
      @ExceptionHandler:指明处理那个异常类
    • 理解:好像一个触发器,运行抛出未处理的异常,就会触发这个类
    package com.gzhj.safecampus.configuration.exceptionhandler;
    
    import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
    import com.gzhj.core.exception.BusinessException;
    import com.gzhj.core.restsupport.ResponseResult;
    import com.gzhj.core.restsupport.ResponseStatuses;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.web.bind.annotation.ControllerAdvice;
    import org.springframework.web.bind.annotation.ExceptionHandler;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import javax.validation.ConstraintViolation;
    import javax.validation.ConstraintViolationException;
    import java.util.List;
    import java.util.stream.Collectors;
    
    /**
     * 全局异常处理类
     *
     * @author Jack
     */
    @ControllerAdvice
    public class GlobalExceptionHandler {
    
        private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
    
        /**
         * 业务异常处理
         *
         * @param exception
         * @return
         */
        @ExceptionHandler(BusinessException.class)
        @ResponseBody
        public ResponseResult businessExceptionHandler(BusinessException exception) {
    
            // 记录到日志文件
            log.error(exception.getMessage(), exception);
    
            return ResponseResult.error(exception.getErrorCode(), exception.getMessage());
        }
    
        /**
         * nullpointer exception处理
         *
         * @param exception
         * @return
         */
        @ExceptionHandler(NullPointerException.class)
        @ResponseBody
        public ResponseResult nullPointExceptionHandler(NullPointerException exception) {
            String errMsg = "未将对象引用设置到对象实例,请检查代码!";
    
            // 记录到日志文件
            log.error(errMsg, exception);
    
            return ResponseResult.error(ResponseStatuses.ERROR_BUSINESS_COMMON.getCode(), errMsg);
        }
    
        /**
         * Mybatis plus Exception异常处理
         *
         * @param exception
         * @return
         */
        @ExceptionHandler(MybatisPlusException.class)
        @ResponseBody
        public ResponseResult mybatisplusExceptionHandler(MybatisPlusException exception) {
            // 记录到日志文件
            log.error("Mybatis plus数据操作出现异常", exception);
    
            return ResponseResult.error(ResponseStatuses.ERROR_BUSINESS_COMMON.getCode(), exception.getMessage());
        }
    
        /**
         * 校验异常处理
         *
         * @return
         * @valid验证异常处理
         */
        @ExceptionHandler(value = ConstraintViolationException.class)
        @ResponseBody
        public ResponseResult ConstraintViolationExceptionHandler(ConstraintViolationException exception) {
    
            // 记录到日志文件
            log.error(exception.getMessage(), exception);
    
            List<String> msgList = exception.getConstraintViolations()
                    .stream()
                    .map(ConstraintViolation::getMessage)
                    .collect(Collectors.toList());
    
            return ResponseResult.error(ResponseStatuses.ERROR_BUSINESS_COMMON.getCode(), msgList.toString());
        }
    
        /**
         * 将Exception信息转为String
         *
         * @param e 异常
         * @return 异常字符串
         */
        private String getExceptionDetailMessage(Exception e) {
            StackTraceElement[] ste = e.getStackTrace();
            StringBuffer sb = new StringBuffer();
            sb.append(e.getMessage() + "\r\n");
    
            for (int i = 0; i < ste.length; i++) {
                sb.append(ste[i].toString() + "\r\n");
            }
    
            return sb.toString();
        }
    }
    
    

    相关文章

      网友评论

          本文标题:spring boot整合全局异常处理

          本文链接:https://www.haomeiwen.com/subject/myqrjktx.html