13

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.

Shivkumar Mallesappa
  • 2,875
  • 7
  • 41
  • 68

2 Answers2

8

Short Answer is to filter response after the doFilter method.

    @Bean
    public Filter myCustomFilter() {
        return (request, response, chain) -> {
            logger.info("do Request Filtering ... ");
            chain.doFilter(request, response); // Do Response Filter after this
            logger.info("do Response Filtering ... ");
        };
    }

Explanation

The Servlet Filters in Servlet Container are invoked in a chain with each Filter invoking the next Filter in the chain through the FilterChain doFilter method. The last filter in the filter chain delegates the request to the actual Servlet which then processes the request and generates the Response. The response then passes through each of those filters in the Filter Chain but with the opposite ordering. So, the last filter becomes the first filter while processing the response and it goes through all the filters back to the Client.

enter image description here

SKumar
  • 1,940
  • 1
  • 7
  • 12
4

You can do that by implementing Filter interface

@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 
         res.setHeader("key", "value");

    chain.doFilter(req, res);
  }

  @Override
  public void destroy() {}

  @Override
  public void init(FilterConfig arg0) throws ServletException {}

}
e2rabi
  • 4,728
  • 9
  • 42
  • 69
  • 4
    I am little bit confused here , I have implemented a Filter, how it is identified whether it is a request filter or a response filter. Like in Jersey in was using `ContainerResponseFilter` – Shivkumar Mallesappa Jan 24 '17 at 10:24
  • Where I can modify my response. – Shivkumar Mallesappa Jan 24 '17 at 10:25
  • Actually I have two filters , Authentication Filter and Authorization Filter and it contains multiple `chain.doFilter` , so can I implement this anywhere ? Secondly I am not understanding the flow , I mean how my response will go through this Filter? Sorry if I am asking something wrong , but I am not able to correlate this with jersey. Thanks for your response – Shivkumar Mallesappa Jan 24 '17 at 10:32
  • 1
    I want to log my API request and responses, I am done with request , facing problem with responses. – Shivkumar Mallesappa Jan 24 '17 at 10:34
  • @ShivkumarMallesappa did you find any solution to your problem? Did you able to execute a particular filter in response.? – pijushcse Mar 17 '18 at 03:52
  • 2
    [This question and answer](https://stackoverflow.com/questions/39935190/contentcachingresponsewrapper-produces-empty-response) resolves the problem and answers your question. – Desislav Kamenov Nov 15 '19 at 10:13
  • @ShivkumarMallesappa any update on this? I also got stuck here and I also come from Jersey – bishwa.poudel Jun 26 '22 at 03:22