Defining a HandlerInterceptor

The following are the methods you can override in HandlerInterceptorAdapter:

  • public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler): Invoked before the handler method is invoked
  • public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView): Invoked after the handler method is invoked
  • public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex): Invoked after the request processing is complete

The following example implementation shows how to create a HandlerInterceptor. Let's start with creating a new class that extends HandlerInterceptorAdapter:

Get Mastering Spring 5.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.