Spring MVC RequestMappingHandlerMapping Interceptor Annotation Example
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.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.AppConfig.java
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; @Configuration @ComponentScan("com.concretepage") @EnableWebMvc public class AppConfig { @Bean public UrlBasedViewResolver setupViewResolver() { UrlBasedViewResolver resolver = new UrlBasedViewResolver(); resolver.setPrefix("/views/"); resolver.setSuffix(".jsp"); resolver.setViewClass(JstlView.class); return resolver; } @Bean public RequestMappingHandlerMapping requestMappingHandlerMapping() { RequestMappingHandlerMapping mapping = new RequestMappingHandlerMapping(); mapping.setInterceptors(new Object[] {getLoggingHandler()}); return mapping; } @Bean public LoggingInterceptor getLoggingHandler(){ return new LoggingInterceptor(); } }
Create Interceptor Class
We are creating one interceptor class using HandlerInterceptorAdapter.LoggingInterceptor.java
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 { @Override 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.WelcomeController.java
package com.concretepage; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/myworld") public class WelcomeController { @RequestMapping("/welcome") public String hello(Model model) { System.out.println("Welcome Friends!"); model.addAttribute("msg", "Welcome Friends!"); return "result"; } }
Output
Run the URLhttp://localhost:8080/concretepage-1/myworld/welcome
And find the output in console.
---Before Method Execution---preHandle() Welcome Friends!