×
DocsConsole

DocsConsole

Spring Web MVC HandlerInterceptor Introduction

By | | Updated : 2018-12-05 | Viewed : 283 times

Spring Web MVC HandlerInterceptor Introduction

Sometimes we need to preprocess or post proses the request based on requirement. So we have to intercept the request to pro/post process request. So here we can use the intercept concept. It is similar to servlet filter.

HandlerInterceptor Introduction

To Pre-Process/Post-Process for certain requests, we should use Interceptor to intercept request in Spring MVC. For Example, if we want to modify or check Principal User object before processing the request, we can manipulate/check Principal User.

    Spring provides two type of interfaces to implement the request interception concept.

  • HandlerInterceptor.

  • HandlerInterceptorAdapter

    Both above mentioned interface have same methods are

  • preHandle(..) is called before the actual handler is executed;

  • postHandle(..) is called after the handler is executed;

  • afterCompletion(..) is called after the complete request has finished.

HandlerInterceptorAdapter vs HandlerInterceptor

In HandlerInterceptor, it will not allow to override the specific methods. Here we need to override the all the three methods. But in HandlerInterceptorAdapter, we can override specific methods. No need to override all the three methods.Please find the diagrammatic representation for interceptor concept.

Spring Web MVC HandlerInterceptor

    Excituion Flow:

  • Once request is received, the Dispatcher Servlet take help from HandlerMapping to get handler. HandlerMapping maps each request with Handler (Controller) and Interceptors

  • HandlerMapping returns HandlerExecutionChain which contains handler, interceptors and handler method's order.Dispatcher servlet will make use of HandlerAdapter to invoke all handler methods as per HandlerExecutionChain.

  • As per HandlerExecutionChain, preHandle() should invoke first. So HandlerAdapter will invoke preHandle().

  • After execution of Handler, HandlerAdapter will again call postHandle() of interceptor.

  • While sending the response to the view, again afterCompletion(..) will be invoked by HandlerAdapter.

Configuration of HandlerInterceptor

Xml configuration for HandlerInterceptor
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

	
	<mvc:interceptors>
		<mvc:interceptor>
			<mvc:mapping path="/getAdminHome" />
			<bean class="com.docsconsole.tutorials.springmvc5.interceptor.AdminRequestInterceptor" />
		</mvc:interceptor>
		<mvc:interceptor>
			<mvc:mapping path="/getUserHome" />
			<bean class="com.docsconsole.tutorials.springmvc5.interceptor.UserRequestInterceptor" />
		</mvc:interceptor>
	</mvc:interceptors>
	
</beans>

    Here we are providing two inner tags in <mvc:interceptor>.

  • 1. <mvc:mapping/> to map the request path which is to be intercept.

  • 2. <bean/> to define the customized interceptor.

Java configuration for HandlerInterceptor
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "com.docsconsole.tutorials.springmvc5"})
public class WebMvcConfig implements WebMvcConfigurer {
 
   //..
   
   @Bean
   AdminRequestInterceptor gdminRequestInterceptor() {
        return new AdminRequestInterceptor();
   }
   
   UserRequestInterceptor userRequestInterceptor() {
       return new UserRequestInterceptor();
  }
   @Override
   public void addInterceptors(InterceptorRegistry registry) {
       registry.addInterceptor(gdminRequestInterceptor()).addPathPatterns("/getAdminHome");
       registry.addInterceptor(userRequestInterceptor()).addPathPatterns("/getUserHome");
   }
  
}

It is equivalent to previous xml configuration for interceptors.

Leave A Reply