Friday, 1 January 2016

Using interceptors in Spring MVC


Interceptors as their name suggest intercepts request that are delegated to your controller by the dispacher setvlet. Why would we do that you ask? Well there are multiple possibilities. You can implement in these interceptors functionality that is common to multiple controllers. Like for eg - 
  • Add common model attributes
  • Set response header
  • Audit requests
  • Measure performance etc
In this post we will see how to implement those interceptors in Spring MVC.

HandlerInterceptor interface

HandlerIntercetor is an interface with following methods - 

  • boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler);
  • void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler. ModelAndView modelAndView);
  • void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler. Exception ex);
Their call sequence is reflected in following picture -


preHandle() returns either true or false -
  • true : continue doewn the interceptor chain
  • false : invoke controller and skip remaining interceptors
Does this also mean you can have multiple interceptors? Precisely Yes! You can chain interceptors and they will be called one after another.

As in most case you don't want to implement all 3 methods so Spring has a separate class called - HandlerInterceptorAdapter which you can easily extend and override those methods that you need.

Configuring Interceptors

- Always keep in mind interceptors are are HandlerMapping level!

Spring as you know has multiple components like
  • HandlerMapping
  • HandlerAdapter
  • ViewResolver
  • HandlerExceptionResolver
 and user define components like
  • Controllers/Handler
  • Interceptors
There are 3 ways to configure interceptors

  1. Define interceptor as a property of your HandlerMapping bean -

    <bean class="...DefaultAnnotationHandlerMapping">
        <preoperty name="interceptors">
                <bean class=""/>
                <bean class=""/>
  2. OR use mvc namespace to define interceptors -

        <bean class="myPackage.Interceptor1"/>
        <bean class="myPackage.Interceptor2"/>

    NOTE : This will be applied to all HandlerMapping beans. If you want to restrict your interceptors to particular HandlerMapping use -
  3. mapping paramter. You can optionally give exclude paramter too (available from Spring 3.1+)
            <mvc:mapping path="/secure/*" />
            <mvc:exclude-mapping path="/secure/help" /> 
            <bean class="" />

Interceptor Example

Lets see an example of how to create an interceptor - 

Lets first create s simple servlet configuration to configure our dispacher Servlet - 

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" version="2.5">



As you can see our dispacher servlet will server all requests that are of format 
  • http://localhost:8080/projectName/admin
 projectName is the name of Spring project you have provided.

Now lets create the Spring configuration. Create a file under /WEB-INF/spring with xml extension and add following contents to it -

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/"/>
        <property name="suffix" value=".jsp"/>

    <context:component-scan base-package="myPackage" />


            <bean class="myPackage.TestInterceptor" />


If you notice we have configured an interceptor to intercept all requests (i.e applicable for all handler mappings)

If you want it to be for specific URL you can do something like - 

            <mvc:mapping path="/**" />
            <bean class="myPackage.TestInterceptor" />


            <mvc:mapping path="/home" />
            <bean class="myPackage.TestInterceptor" />

Finally lets create our Controller and interceptor -


public class WelcomeController {
    @RequestMapping(value="/home", method = RequestMethod.GET)
    public String welcome() {
        return "welcome";


public class TestInterceptor extends HandlerInterceptorAdapter {
    public boolean preHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler) throws Exception {
        System.out.println("Request intercepted");
        return true;

After you have done this setup you can hit -
  • http://localhost:8080/projectName/admin/home
and see  "Request intercepted" printed in the logs. Also you can define multiple such <mvc:interceptor> tags thereby chaining them.

Also not interceptors will be hit only if the path that you are hitting is valid. Si if you try /home/test then interceptor will not be hit. It will be hit only if the requested path forms a part of valid HandlerMapping (like /home in above case).

Also will remind you again interceptors are configured at HandlerMapping level. In this case it would be RequestMappingHandlerMapping (Spring 3.1+ with mvc:annotation-driven) or DefaultAnnotationHandlerMapping.

Related Links

Difference between "/*" and "/**" in Spring MVC paths


There are many places in Spring configuration where we need to provide path. It might be a mapping or a resource path. For example an interceptor mapping - 

        <mvc:mapping path="/secure/**" />
        <mvc:exclude-mapping path="/secure/help" /> 
        <bean class="" />

OR Servlet mapping in web.xml


or even your resource chain

<mvc:resources mapping="/css/**" location="/css/">
    <mvc:resource-chain resource-cache="true" auto-registration="true">
                <mvc:content-version-strategy patterns="/**"/>

In all these place you can use  "/*" or "/**". But the million dollar question is what does each mean and when do we use what?

Difference between "/*" and "/**" in Spring MVC paths

  • An asterisk ('*') matches zero or more characters, up to the occurrence of a '/' character (which serves as a path separator). A string, such as "/abcd/docs/index.html", would not match successfully against the pattern '/*/*.index.html'. The first asterisk matches up to the first path separator only, resulting in the "abcd" string. A successful matching pattern would be '/*/*/*.html'.
  • A string containing two asterisks ('**') matches zero or more characters. This could include the path separator '/'. In this case, "/abcd/docs/index.html" would successfully match the '/**/*.html' pattern. The double asterisk, including the path separator, would match the "abcd/docs" string.

So to sum up "/**" takes into account even the subpaths that may include the "/" i.e the path separator.


This is a path pattern that used in Apache ant, spring team implement it and use it throughout the framework.

The mapping matches URLs using the following rules:

  • ? matches one character
  • * matches zero or more characters
  • ** matches zero or more 'directories' in a path
Some examples:

  • com/t?st.jsp - matches com/test.jsp but also com/tast.jsp or com/txst.jsp
  • com/*.jsp - matches all .jsp files in the com directory
  • com/**/test.jsp - matches all test.jsp files underneath the com path
  • org/springframework/**/*.jsp - matches all .jsp files underneath the org/springframework path
  • org/**/servlet/bla.jsp - matches org/springframework/servlet/bla.jsp but also org/springframework/testing/servlet/bla.jsp and org/servlet/bla.jsp


Related Links

t> UA-39527780-1 back to top