java 异常 过滤器_springboot如何处理过滤器filter中抛出的异常

importcom.yzf.enterprise.market.common.constant.HttpStatus;importcom.yzf.enterprise.market.common.exception.BaseException;importcom.yzf.enterprise.market.common.exception.CustomException;importcom.yzf.enterprise.market.common.exception.DemoModeException;importcom.yzf.enterprise.market.common.utils.StringUtils;importcom.yzf.enterprise.market.framework.web.domain.AjaxResult;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importorg.springframework.security.access.AccessDeniedException;importorg.springframework.security.authentication.AccountExpiredException;importorg.springframework.security.core.userdetails.UsernameNotFoundException;importorg.springframework.validation.BindException;importorg.springframework.web.bind.MethodArgumentNotValidException;importorg.springframework.web.bind.annotation.ExceptionHandler;importorg.springframework.web.bind.annotation.RestControllerAdvice;importorg.springframework.web.servlet.NoHandlerFoundException;/*** 全局异常处理器*/@RestControllerAdvicepublic classGlobalExceptionHandler {private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);/*** 基础异常*/@ExceptionHandler(BaseException.class)publicAjaxResult baseException(BaseException e) {returnAjaxResult.error(e.getMessage());

}/*** 业务异常*/@ExceptionHandler(CustomException.class)publicAjaxResult businessException(CustomException e) {if(StringUtils.isNull(e.getCode())) {returnAjaxResult.error(e.getMessage());

}returnAjaxResult.error(e.getCode(), e.getMessage());

}

@ExceptionHandler(NoHandlerFoundException.class)publicAjaxResult handlerNoFoundException(Exception e) {

log.error(e.getMessage(), e);return AjaxResult.error(HttpStatus.NOT_FOUND, "路径不存在,请检查路径是否正确");

}

@ExceptionHandler(AccessDeniedException.class)publicAjaxResult handleAuthorizationException(AccessDeniedException e) {

log.error(e.getMessage());return AjaxResult.error(HttpStatus.FORBIDDEN, "没有权限,请联系管理员授权");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值