简体   繁体   中英

Execute GenericFilterBean only on response with Spring boot 2.0.0

I have seen that if I register a Spring GenericFilterBean class, it invoked every time on request. Is it possible to invoke another filter (maybe another GeneircFilterBean ) only when service is returning the response? Basically, I wanted to achieve the same behavior as Jersey ContainerResponseFilter .

Thanks in advance

You can pass you code after chain.doFilter(request, response) method. Here is example.

public class SimpleLoggingFilter extends GenericFilterBean {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        chain.doFilter(request, response);
//This code will work after response
        if ((response instanceof HttpServletResponse) && (request instanceof HttpServletRequest))  {           
            HttpServletResponse httpServletResponse = (HttpServletResponse) response;
            System.out.println("Response status is " + httpServletResponse.getStatus() +" " +
                    "Response  content-length is " + httpServletResponse.getHeader("content-length"));
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM