exception handling for filter in spring

后端 未结 7 868
既然无缘
既然无缘 2020-12-17 14:59

I am handling exceptions in spring using @ExceptionHandler. Any exception thrown by controller is caught using method annotated with @ExceptionHandler and action is taken ac

相关标签:
7条回答
  • 2020-12-17 16:05

    This is what I did in my filter class to throw error:

        @Override
        public void doFilter(ServletRequest request, ServletResponse response,
                FilterChain chain)
                throws IOException, ServletException {
            HttpServletRequest req = (HttpServletRequest) request;
            if (req.getHeader("Content-Type") == null) {
                HttpServletResponse httpResponse = (HttpServletResponse) response;                
                httpResponse.sendError(HttpServletResponse.SC_BAD_REQUEST,
                       "Required headers not specified in the request");            
            }
            chain.doFilter(request, response);
        }
    
    0 讨论(0)
提交回复
热议问题