Spring boot 过滤器 filter中 关于@RestControllerAdvice无法捕获过滤器中的异常 解决

/**
 * @author kong
 */
@WebFilter
@Slf4j
public class RefactorRequestFilter implements Filter {
	//此处声明异常全局处理
    @Autowired
    private HandlerExceptionResolver handlerExceptionResolver;
 @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        if (request.getRequestURI().contains("/v2/cast")) {
            //获取request的body参数
            final String body = getBodyString(request);
            byte[] data;
            try {
            	//将handler中抛出的异常try起来
                data = handler(body);
            } catch (Exception e) {
            	//交给全局异常处理后  结束执行  ErrorException为自定义的异常
                handlerExceptionResolver.resolveException(request, response, null, new ErrorException(e.getMessage()));
                return;
            }
            chain.doFilter(new RefactorRequestWrapper(request, data), response);
        } else {
            chain.doFilter(request, response);
        }

    }

你可能感兴趣的:(Java,springboot,java)