Show List

Interceptors and Filters in Spring MVC

Interceptors and Filters are two mechanisms in Spring MVC for processing requests and responses.

Interceptors are components in Spring MVC that provide a mechanism for pre- and post-processing of requests. They are defined as classes annotated with @Component and implement the HandlerInterceptor interface. The HandlerInterceptor interface has three methods:

  • preHandle: This method is called before the request is handled by the controller. You can use this method to perform any pre-processing on the request, such as authentication and authorization.
  • postHandle: This method is called after the request has been handled by the controller. You can use this method to modify the model or perform any post-processing on the request.
  • afterCompletion: This method is called after the response has been generated and sent back to the client. You can use this method to perform any cleanup or logging.

For example, to define a simple interceptor that logs the start and end time of each request:

@Component
public class LogInterceptor implements HandlerInterceptor {

  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
    System.out.println("Start Time: " + new Date());
    return true;
  }

  @Override
  public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
    System.out.println("End Time: " + new Date());
  }

  @Override
  public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
    // do nothing
  }
}

Filters are similar to interceptors, but they operate at a lower level and provide a more general-purpose mechanism for processing requests and responses. They are defined as classes annotated with @Component and implement the Filter interface. The Filter interface has three methods:

  • init: This method is called when the filter is initialized and can be used to perform any setup.
  • doFilter: This method is called for each request and response and can be used to perform any processing on the request and response, such as security and compression.
  • destroy: This method is called when the filter is destroyed and can be used to perform any cleanup.

For example, to define a simple filter that sets the response header:

@Component
public class HeaderFilter implements Filter {

  @Override
  public void init(FilterConfig filterConfig) throws ServletException {
    // do nothing
  }

  @Override
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    HttpServletResponse httpResponse = (HttpServletResponse) response;
    httpResponse.setHeader("X-Powered-By", "Spring Boot");
    chain.doFilter(request, response);
  }

  @Override
  public void destroy() {
    // do nothing
  }
}
By using interceptors and filters, you can add custom processing to your application, such as logging, security, and compression, without modifying the controller logic. This makes it easier to maintain and test your application and provides greater flexibility in the processing of requests and responses.

    Leave a Comment


  • captcha text