Spring MVC Interceptor HandlerInterceptorAdapter, HandlerInterceptor Example

Spring Interceptor are used to intercept client requests and process them. Sometimes we want to intercept the HTTP Request and do some processing before handing it over to the controller handler methods. That’s where Spring MVC Interceptor come handy.

Spring Interceptor

Just like we have Struts2 Interceptors, we can create our own Spring interceptor by either implementing org.springframework.web.servlet.HandlerInterceptor interface or by overriding abstract class org.springframework.web.servlet.handler.HandlerInterceptorAdapter that provides the base implementation of HandlerInterceptor interface.

Spring Interceptor – HandlerInterceptor

Spring HandlerInterceptor declares three methods based on where we want to intercept the HTTP request.

  1. boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler): This method is used to intercept the request before it’s handed over to the handler method. This method should return ‘true’ to let Spring know to process the request through another spring interceptor or to send it to handler method if there are no further spring interceptors.If this method returns ‘false’ Spring framework assumes that request has been handled by the spring interceptor itself and no further processing is needed. We should use response object to send response to the client request in this case.Object handler is the chosen handler object to handle the request. This method can throw Exception also, in that case Spring MVC Exception Handling should be useful to send error page as response.
  2. void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView): This HandlerInterceptor interceptor method is called when HandlerAdapter has invoked the handler but DispatcherServlet is yet to render the view. This method can be used to add additional attribute to the ModelAndView object to be used in the view pages. We can use this spring interceptor method to determine the time taken by handler method to process the client request.
  3. void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex): This is a HandlerInterceptor callback method that is called once the handler is executed and view is rendered.

If there are multiple spring interceptors configured, preHandle() method is executed in the order of configuration whereas postHandle() and afterCompletion() methods are invoked in the reverse order.

Let’s create a simple Spring MVC application where we will configure an Spring Interceptor to log timings of controller handler method.

Our final Spring Interceptor example project will look like below image, we will look into the components that we are interested in.


Spring Interceptor – Controller Class

I am just adding some processing time in the execution of the handler method to check our spring interceptor methods in action.

Spring MVC Interceptor – HandlerInterceptorAdapter Implementation

For simplicity, I am extending abstract class HandlerInterceptorAdapter. HandlerInterceptorAdapter is abstract adapter class for the HandlerInterceptor interface, for simplified implementation of pre-only/post-only interceptors.

The logic is really simple, I am just logging the timings of handler method execution and total time taken in processing the request including rendering view page.

Spring MVC Interceptor Configuration

We have to wire the spring interceptor to the requests, we can use mvc:interceptors element to wire all the interceptors. We can also provide URI pattern to match before including the spring interceptor for the request through mapping element.

Our final spring bean configuration file (spring.xml) looks like below.

I will not explain all other components of the web application, because we are not interested in them and they don’t have any specific spring interceptor related configuration.

Spring MVC Interceptor Application Testing

Just deploy the application in servlet container and invoke the home controller, you will see logger output something like below.

The output confirms that the spring interceptor methods are executed in the order defined.

That’s all for using spring interceptors, you can download the Spring Interceptor example project from below link and try to have multiple interceptors and check by different order of configuration.

By admin

Leave a Reply

%d bloggers like this: