What is difference between HandlerInterceptor and HandlerInceptorAdaptor in Spring MVC?

It's always a good practice to Program to an interface, not an implementation and Spring Framework uses this practice by providing quite a lot of these interfaces, HandlerInterceptor is one of them. Some of these interfaces are richer than others. So if you as a client want to provide a custom implementation for them and only care for a few of their methods, you would end up with a few actual implementations and a lot of empty implementations.

For example, suppose you want to provide an implementation for preHandle method and don't care about the other two. Unfortunately you should provide some empty implementations for the other two:

public class CustomHandlerInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, 
                             Object handler) throws Exception {
        // Some complex logic
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response,
                           Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
                                Object handler, Exception ex) throws Exception {

    }
}

Those empty implementations would cause greater boilerplate codes when you're implementing even more richer interfaces, like WebMvcConfigurer with 10+ abstract methods, imagine the loads of empty methods.

In order to solve this problem, Spring Framework usually provides a corresponding abstract Adapter for those interfaces, like HandlerInterceptorAdaptor for HandlerInterceptor interface or WebMvcConfigurerAdapter for WebMvcConfigurer. Those adapters are just a bunch of default and simplified implementations for all methods of those interfaces. You can refactor the preceding code using the provided adapter:

public class CustomHandlerInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
                             Object handler) throws Exception {
        // Some complex logic
    }
}

Since those empty implementations are provided by the HandlerInterceptorAdapter class, you just need to provide your implementation for preHandle method.

As I said, this is a recurring theme in the Spring Framework, some of the common examples are:

  • WebMvcConfigurer and WebMvcConfigurerAdapter
  • CachingConfigurer and CachingConfigurerSupport

HandlerInterceptor is an interface that defines a contract for doing stuff. It has 3 abstract methods that need to be implemented.

Even if you only need to intercept afterCompletion, you still need 2 empty methods!

In Spring parlance, an "Adapter" is an abstract class that creates default empty implementations for all methods, so that you only need to override the ones you want. Note, this predates the Java 8 default method that would render this redundant.

If you only need preHandle and postHandle functionality, then you should use HandlerInterceptorAdaptor and you will only need to override those two methods. If you use a plain HandlerInterceptor, you will need still need one empty method for the code to compile - this is needless boilerplate.


Since java 8 have provided a great feature of default implementations for interface methods, Interfaces in newer version of spring do not need adapter classes to provide default implementation of interface.

So, we can directly implement interface and override only required methods from interface, instead of extending corresponding adapter class for spring interface.

For example, if you check declaration of HandlerInterceptor interface, all of it's methods have empty implementations, so no need of it's adapter cass HandlerInterceptorAdapter