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());
        }
    
    }
  • 相关阅读:
    day22-20180522笔记
    day20-20180517笔记
    day19-20180515笔记
    day18-20180513笔记
    day17-20180510笔记
    day16-20180508笔记
    Python之初识面向对象
    Python之常用模块(2)
    Python之常用模块(1)
    Python之模块与包(下)
  • 原文地址:https://www.cnblogs.com/rchao/p/13853430.html
Copyright © 2011-2022 走看看