Home  >  Spring  >  Spring MVC

Spring MVC RequestMappingHandlerMapping Interceptor Annotation Example

By Arvind Rai, April 02, 2015
In this page we will learn spring MVC RequestMappingHandlerMapping interceptor annotation example. RequestMappingHandlerMapping scans all @RequestMapping annotations in all controller classes. It provides a method setInterceptors() to add interceptors. We cannot set path pattern here but can be set order of execution. Find the example.

Required Software

Find the required software and tools.
1. Java 7
2. Tomcat 8
3. Eclipse
4. Gradle
5. Spring 4

Project Structure in Eclipse

Find the project structure in eclipse.
Spring MVC RequestMappingHandlerMapping Interceptor Annotation Example

Configuration File for RequestMappingHandlerMapping

RequestMappingHandlerMapping automatically searches for @RequestMapping annotations in all controller classes annotated with @Controller. In java configuration class, we need to implement WebMvcConfigurationSupport.requestMappingHandlerMapping(). If we annotate our class with @EnableWebMvc annotation, we need not to extend WebMvcConfigurationSupport. To add interceptor, it provides a method setInterceptors() . We can add more than one interceptor. We cannot assign path patterns here. They work for all patterns. We can set order of interceptors using setOrder() method.
package com.concretepage.config;  
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.UrlBasedViewResolver;
import com.concretepage.interceptors.LoggingInterceptor;
public class AppConfig {  
        public UrlBasedViewResolver setupViewResolver() {  
           UrlBasedViewResolver resolver = new UrlBasedViewResolver();  
           return resolver;  
	public RequestMappingHandlerMapping requestMappingHandlerMapping() {
	   RequestMappingHandlerMapping mapping = new RequestMappingHandlerMapping();
	   mapping.setInterceptors(new Object[] {getLoggingHandler()});
	   return mapping;
	public LoggingInterceptor getLoggingHandler(){
	   return new LoggingInterceptor();

Create Interceptor Class

We are creating one interceptor class using HandlerInterceptorAdapter.
package com.concretepage.interceptors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
public class LoggingInterceptor extends HandlerInterceptorAdapter  {
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
		throws Exception {
		System.out.println("---Before Method Execution---preHandle()");
		return true;

Create Controller

Find the controller class.
package com.concretepage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
public class WelcomeController {
        public String hello(Model model) {
	    System.out.println("Welcome Friends!");
	    model.addAttribute("msg", "Welcome Friends!");
            return "result";


Run the URL
And find the output in console.
---Before Method Execution---preHandle()
Welcome Friends! 

Download Complete Source Code


©2021 concretepage.com | Privacy Policy | Contact Us