title: Interceptor in Spring 5 WebFlux
body: <p>I am using <code>Spring WebFlux</code> in my project. I want to create an interceptor to calculate the time taken by each API. In <code>Spring MVC</code> we have <code>HandlerInterceptor</code> which is not present in <code>spring-boot-starter-webflux</code>. I tried adding <code>spring-boot-starter-web</code> and wrote my interceptor but it didn't work. Here is the code:</p>
  
  <pre><code>@Component
  public class TimeInterceptor implements HandlerInterceptor {
  
  public static Logger logger = Logger.getLogger(TimeInterceptor.class);
  
  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
      long startTime = System.currentTimeMillis();
      request.setAttribute("startTime", startTime);
      return true;
  }
  
  @Override
  public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
      long totaltime = System.currentTimeMillis() - (long) request.getAttribute("startTime");
      request.setAttribute("totaltime", totaltime);
      logger.info("Logging total time" + totaltime);
  
  }
  ...
  ...
  </code></pre>
  
  <p>I want to add similar functionality to my application and intercept time taken by each call.</p>
  
  <p>Thanks in advance.</p>