In this article we will create a simple Spring MVC Interceptor example.
Spring MVC Interceptor
As the name suggests, Spring MVC Interceptors are java classes that are used to intercept any requests. Interceptors are configurable and can be configured on all the requests OR a group of requests. We can configure multiple requests based on our requirements. While configuring a interceptors we have exclude some of the requests that need not go through the interceptors.
Use of Interceptor
Interceptors are generally used for authorization of requests, common loggins done before and after a request is processed.
How to create a Spring MVC Interceptor
In spring, Interceptors are java class that either implement HandlerInterceptor or extend HandlerInterceptorAdaptor
HandlerInterceptorAdaptor is a spring class that allows the its sub class to only override the below three required methods from the HandlerInterceptor interface.
Extending HandlerInterceptorAdaptor is more convenient than implementing the HandlerInterceptor interface, as the HandlerInterceptorAdaptor has already implemented the interface methods and we need to only override the methods we need to add logic to.
3 important methods that we need to understand about the interceptors are
preHandle(..)
This method returns a boolean value. It is called just before the controller call. You can use this method to break or continue the processing of the execution chain.
If the method returns true, the handler execution chain will continue i.e. the controller will be called.
If the method returns false, the DispatcherServlet assumes the interceptor itself has taken care of requests and does not continue executing.
postHandle(..)
This method is called just after the controller call. You can use this method to add extra objects/attributes to Model object that will be send as a response.
afterCompletion(..)
This method is called after complete request is finished.
If there are multiple interceptors configured, preHandle() method is executed in the order of configuration whereas postHandle() and afterCompletion() methods are executed in the reverse order.
Now lets create a Spring MVC Interceptor Example.
In the attached example i have create 2 controller, home and employee. We will create a interceptor to log details when a request comes in. We will configure the interceptor to allow requests coming to HomeController to intercepted , but not to the EmployeedController.
Create Interceptor
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
package com.kscodes.example.interceptors; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Component; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; @Component public class LoggingInterceptor extends HandlerInterceptorAdapter { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("preHandle :: Request inside preHandle"); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("postHandle :: Request inside postHandle"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("afterCompletion :: Request inside afterCompletion"); } } |
Configure the Interceptor in dispatcher servlet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <context:component-scan base-package="com.kscodes.example" /> <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/home" /> <mvc:exclude-mapping path="/employee" /> <bean class="com.kscodes.example.interceptors.LoggingInterceptor" /> </mvc:interceptor> </mvc:interceptors> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/jsp/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> </beans> |
Output
When you access the below URL you will see output at the console
http://localhost:8080/interceptorexample/home
and when you use the employee url no output will be logged as no interceptor will be called
http://localhost:8080/interceptorexample/employee