I have worked with JERSEY framework , it provides feature to implement a filter so that all the responses will go through it.
I am new to Spring / Spring boot. I am not understanding how to achieve the above functionality which I mentioned.
Basically I want my each Response should pass through my filter.
How to do this ?
A sample example will be helpful.
If I implemented as follows as @Errabi Ayoub suggested:
@Component
public class MyClassFilter implements Filter {
@Override
public void doFilter( HttpServletRequest req, HttpServletResponse res,
FilterChain chain) throws IOException, ServletException {
// you can modify your response here before the call of chain method
//example
apiLogger.logResponse();
res.setHeader("key", "value");
chain.doFilter(req, res);
}
@Override
public void destroy() {}
@Override
public void init(FilterConfig arg0) throws ServletException {}
}
and I have a method apiLogger.logResponse();
then my method will be called twice, according to my logic, first it will be called at request and then again on response. I don't want that. I want to log only when it is Response.
Thanks.