zoukankan      html  css  js  c++  java
  • springboot controller统一异常处理

    @ControllerAdvice


    @RestControllerAdvice //纯接口系统可以使用
    我的系统只有api接口 使用了RestControllerAdvice
    @RestControllerAdvice
    public class BaseExceptionHandler {
    
        private static final Logger logger = LoggerFactory.getLogger(BaseExceptionHandler.class);
    
        @ExceptionHandler(value = Exception.class)
        public Result exception(Exception e) {
            //e.printStackTrace();
            //logger.error(String.valueOf(e.getLocalizedMessage()));
            if (e.getCause() != null) {
                if (e.getCause().toString().startsWith("java.sql.SQLIntegrityConstraintViolationException:")) {
                    return Result.err(StatusCode.REP_ERR, "重复操作");
                }
            }
    
            if (e.toString().startsWith(NoLoginException.class.getName())) {
                //未认证
                return Result.err(StatusCode.LOGIN_ERR, e.getMessage());
            }
    
            if (e.toString().startsWith(NoPermissionException.class.getName())) {
                //权限不足
                return Result.err(StatusCode.ACCESS_ERR, e.getMessage());
            }
    
            if(e.toString().startsWith(ExpiredJwtException.class.getName())){
                return Result.err(StatusCode.LOGIN_ERR,"您的登录状态过期了请重新登录");
            }
    
            return Result.err(StatusCode.REMOTE_ERR, e.getMessage());
        }
    
    }
  • 相关阅读:
    第 12 章 Docker Swarm
    第 1 章 虚拟化
    第 0 章 写在最前面
    第 11 章 日志管理
    第 11 章 日志管理
    第 11 章 日志管理
    第 11 章 日志管理
    第 11 章 日志管理
    第 11 章 日志管理
    第 11 章 日志管理
  • 原文地址:https://www.cnblogs.com/rchao/p/13853430.html
Copyright © 2011-2022 走看看