• SpringBoot处理全局统一异常


    import com.spSystem.common.exception.CustomException;
    import com.spSystem.common.exception.response.Result;
    import com.spSystem.common.exception.response.ResultCode;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.validation.BindException;
    import org.springframework.validation.BindingResult;
    import org.springframework.validation.ObjectError;
    import org.springframework.web.bind.MethodArgumentNotValidException;
    import org.springframework.web.bind.annotation.ExceptionHandler;
    import org.springframework.web.bind.annotation.RestControllerAdvice;
    
    import java.util.List;
    
    /**
     * 全局异常处理器
     */
    @RestControllerAdvice
    public class GlobalExceptionHandler {
    
        private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
    
        /**
         * 处理自定义异常
         */
        @ExceptionHandler(CustomException.class)
        public Result handleException(CustomException e) {
            // 打印异常信息
            log.error("### 异常信息:{} ###", e.getMessage());
            return new Result(e.getResultCode());
        }
    
        /**
         * 参数错误异常
         */
        @ExceptionHandler({MethodArgumentNotValidException.class, BindException.class})
        public Result handleException(Exception e) {
    
            if (e instanceof MethodArgumentNotValidException) {
                MethodArgumentNotValidException validException = (MethodArgumentNotValidException) e;
                BindingResult result = validException.getBindingResult();
                StringBuffer errorMsg = new StringBuffer();
                if (result.hasErrors()) {
                    List<ObjectError> errors = result.getAllErrors();
    //                errors.forEach(p ->{
    //                    FieldError fieldError = (FieldError) p;
    //                    errorMsg.append(fieldError.getDefaultMessage()).append(",");
    //                    log.error("### 请求参数错误:{"+fieldError.getObjectName()+"},field{"+fieldError.getField()+ "},errorMessage{"+fieldError.getDefaultMessage()+"}"); });
                }
            } else if (e instanceof BindException) {
                BindException bindException = (BindException)e;
                if (bindException.hasErrors()) {
                    log.error("### 请求参数错误: {}", bindException.getAllErrors());
                }
            }
    
            return new Result(ResultCode.PARAM_IS_INVALID);
        }
    
        /**
         * 处理所有不可知的异常
         */
        @ExceptionHandler(Exception.class)
        public Result handleOtherException(Exception e){
            //打印异常堆栈信息
            e.printStackTrace();
            // 打印异常信息
            log.error("### 不可知的异常:{} ###", e.getMessage());
            return new Result(ResultCode.SYSTEM_INNER_ERROR);
        }
    
    }
  • 相关阅读:
    Android之ToolBar的使用
    Android之 RecyclerView,CardView 详解和相对应的上拉刷新下拉加载
    Andorid 之日历控件,可左右滑动,包含公历,农历,节假日等
    Docker技术入门与实战 第二版-学习笔记-4-Dockerfile外其他生成镜像的方法
    Docker技术入门与实战 第二版-学习笔记-3-Dockerfile 指令详解
    Docker技术入门与实战 第二版-学习笔记-2-镜像构建
    Docker技术入门与实战 第二版-学习笔记-1-镜像
    docker官方文档学习-1-Docker for mac安装配置
    vagrant up下载box慢的解决办法
    主机ping不通virtualbox虚拟机的解决办法
  • 原文地址:https://www.cnblogs.com/jingjiren/p/12966795.html
Copyright © 2020-2023  润新知