Why can't global exception handling handle exceptions thrown in the filter?
My code is looking like this at the moment:
Filter method
@Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest; String token = httpServletRequest.getHeader("token"); if (token==null) throw new NoTokenException(); try { String s = jwtToken.parseJwt(token); httpServletRequest.setAttribute("id",s); }catch (Exception e){ throw new RuntimeException(); } filterChain.doFilter(servletRequest,servletResponse); }
Controller advice class
@RestControllerAdvice public class GlobalExceptionHandle { @ExceptionHandler(Exception.class) public void Handle(Exception e, HttpServletResponse response) throws IOException { response.setContentType("application/json;charset=UTF-8"); response.getWriter().write("msg"); response.flushBuffer(); } }