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
Check the below code snippet, it works for me.
final HttpServletResponseWrapper wrapper = new
HttpServletResponseWrapper((HttpServletResponse) res);
wrapper.sendError(HttpServletResponse.SC_UNAUTHORIZED, "");
res = wrapper.getResponse();
If you are using this inside a filter then add a return statement else chain.doFilter(req,res) will override this.