springMVC中过滤器抛出异常,自定义异常捕获

在过滤器中引入org.springframework.web.servlet.HandlerExceptionResolver

    @Autowired
    @Qualifier("handlerExceptionResolver")
    private HandlerExceptionResolver resolver;
// doFilter中处理
            if (条件1) {
                if (条件2) {
                    resolver.resolveException(request, response, null, new Myself403ForbiddenException("403ForbiddenException"));
                    return;
                }
                filterChain.doFilter(servletRequest, servletResponse);
                return;
            }



Spring Boot全局异常处理@RestControllerAdvice只会捕获所有Controller层抛出的异常,在filter当中抛出的异常GlobalExceptionHandler类是感知不到的,所以在filter当中抛出的异常最终会被Spring框架自带的全局异常处理类BasicErrorController捕获

附:https://blog.csdn.net/m0_37731470/article/details/116754395

你可能感兴趣的:(java)