zoukankan      html  css  js  c++  java
  • springboot如何处理过滤器filter中抛出的异常

    在使用springboot过程中,一般都会设置全局异常管理,如下:

    import com.yzf.enterprise.market.common.constant.HttpStatus;
    import com.yzf.enterprise.market.common.exception.BaseException;
    import com.yzf.enterprise.market.common.exception.CustomException;
    import com.yzf.enterprise.market.common.exception.DemoModeException;
    import com.yzf.enterprise.market.common.utils.StringUtils;
    import com.yzf.enterprise.market.framework.web.domain.AjaxResult;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.security.access.AccessDeniedException;
    import org.springframework.security.authentication.AccountExpiredException;
    import org.springframework.security.core.userdetails.UsernameNotFoundException;
    import org.springframework.validation.BindException;
    import org.springframework.web.bind.MethodArgumentNotValidException;
    import org.springframework.web.bind.annotation.ExceptionHandler;
    import org.springframework.web.bind.annotation.RestControllerAdvice;
    import org.springframework.web.servlet.NoHandlerFoundException;
    
    /**
     * 全局异常处理器
     */
    @RestControllerAdvice
    public class GlobalExceptionHandler {
        private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
    
        /**
         * 基础异常
         */
        @ExceptionHandler(BaseException.class)
        public AjaxResult baseException(BaseException e) {
            return AjaxResult.error(e.getMessage());
        }
    
        /**
         * 业务异常
         */
        @ExceptionHandler(CustomException.class)
        public AjaxResult businessException(CustomException e) {
            if (StringUtils.isNull(e.getCode())) {
                return AjaxResult.error(e.getMessage());
            }
            return AjaxResult.error(e.getCode(), e.getMessage());
        }
    
        @ExceptionHandler(NoHandlerFoundException.class)
        public AjaxResult handlerNoFoundException(Exception e) {
            log.error(e.getMessage(), e);
            return AjaxResult.error(HttpStatus.NOT_FOUND, "路径不存在,请检查路径是否正确");
        }
    
        @ExceptionHandler(AccessDeniedException.class)
        public AjaxResult handleAuthorizationException(AccessDeniedException e) {
            log.error(e.getMessage());
            return AjaxResult.error(HttpStatus.FORBIDDEN, "没有权限,请联系管理员授权");
        }
    }

    这样在系统报错的时候,就能将异常格式化输出到前端,对前端非常友好。但是过滤器中的异常通过这种方式是解决不了的,可以通过以下方式解决:

    import org.springframework.boot.autoconfigure.web.ErrorProperties;
    import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController;
    import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
    import org.springframework.http.HttpStatus;
    import org.springframework.http.MediaType;
    import org.springframework.http.ResponseEntity;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import javax.servlet.http.HttpServletRequest;
    import java.util.Map;
    
    @RestController
    public class TokenErrorController extends BasicErrorController {
    
        public TokenErrorController() {
            super(new DefaultErrorAttributes(), new ErrorProperties());
        }
    
        @Override
        @RequestMapping(produces = {MediaType.APPLICATION_JSON_VALUE})
        public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
            Map<String, Object> body = getErrorAttributes(request, isIncludeStackTrace(request, MediaType.ALL));
            HttpStatus status = getStatus(request);
            return new ResponseEntity((String) body.get("message"), status);
        }
    }

    可以按照自定义的格式定义过滤器异常返回的数据格式。

  • 相关阅读:
    SQL Server 数据库部分常用语句小结(三)
    SQL Server 数据库部分常用语句小结(四)
    通过存储过程(SP)实现SQL Server链接服务器(LinkServer)的添加
    pcb布线强弱电间隔距离
    程序占用内存大小
    Offer来了(原理篇)笔记之第三章并发编程
    Offer来了(原理篇)笔记之第一章JVM原理
    西瓜视频奇妙的bug
    mongodb忘记了admin的账号密码
    MongoDB更改默认端口
  • 原文地址:https://www.cnblogs.com/alimayun/p/13150756.html
Copyright © 2011-2022 走看看