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());
        }
    
    }
  • 相关阅读:
    Part 1R 函数、极限和连续
    Part 1 函数、极限与连续
    C++继承与派生
    VUE笔记
    VUE错误记录
    VUE笔记
    VUE笔记
    VUE笔记
    JS学习笔记
    Node.js笔记 请求方式 GET
  • 原文地址:https://www.cnblogs.com/rchao/p/13853430.html
Copyright © 2011-2022 走看看