I need to add the header in each response. I am planning to do below
public class MyFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
filterChain.doFilter(request, response);
response.addHeader("Access-Control-Allow-Origin", "*");
}
}
I would like to do it after filterChain.doFilter(request, response)
so that once controller process it, i just add header before returning
to client. Is it correct ?
But As per How to write response filter?
After
chain.doFilter
has returned, it's too late to do anything with the response. At this point, entire response was already sent to the client and your code has no access to it.
Above statement does not look right to me. Can't i add header after filterChain.doFilter(request, response)
? If not why ?
i am using spring mvc.