How to use SpringMVC HandlerInterceptor with Spring Boot

Spring MVC HandlerInterceptor provides a mechanism to intercept requests: pre-processing & post-processing. In the Spring MVC HandlerInterceptor tutorial, JavaSampleApproach will guide you how to use HandlerInterceptor by a Logging sample.


I. Technologies

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.5.6.RELEASE

II. Overview
1. Project Structure

springmvc HandlerInterceptor project structure

2. Step to do

– Create Spring Boot project
– Create HandlerInterceptor
– Register HandlerInterceptor
– Create Web Controller
– Run & Enjoy result

III. Practice
1. Create Spring Boot project

– Open Spring Tool Suite, on main menu, choose File->New->Spring Starter Project, add project info. Press Next, then Finish, a Spring Boot project will be created successfully.

Open pom.xml file, add Web dependency:

2. Create HandlerInterceptor

Create LogInterceptor class that implements the interface: HandlerInterceptor
HandlerInterceptor: to customize handler execution chains. Applications can register any number of existing or custom interceptors for certain groups of handlers, to add common preprocessing behavior without needing to modify each handler implementation.

preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception: Intercept the execution of a handler. Called after HandlerMapping determined an appropriate handler object, but before HandlerAdapter invokes the handler.

postHandle(HttpServletRequest request, HttpServletResponse response, Object object, ModelAndView model) throws Exception: Intercept the execution of a handler. Called after HandlerAdapter actually invoked the handler, but before the DispatcherServlet renders the view. Can expose additional model objects to the view via the given ModelAndView.

afterCompletion(HttpServletRequest request, HttpServletResponse response, Object object, Exception arg3) throws Exception: Callback after completion of request processing, that is, after rendering the view. Will be called on any outcome of handler execution, thus allows for proper resource cleanup.

3. Register HandlerInterceptor

Create a configuration class AppConfig that inherits from WebMvcConfigurerAdapter.

WebMvcConfigurerAdapter is an implementation of WebMvcConfigurer with empty methods allowing sub-classes to override only the methods they’re interested in.

WebMvcConfigurer is to define callback methods to customize the Java-based configuration for Spring MVC enabled via @EnableWebMvc.

4. Create Web Controller

Create a simple Spring MVC web controller with only one @RequestMapping(“/hello”)

5. Run & Enjoy result

Build & Run the project with Spring Boot App mode.
Make a request: http://localhost:8080/hello
Logs:

IV. Sourcecode

SpringMVCHandlerInterceptor


Related Posts


2 thoughts on “How to use SpringMVC HandlerInterceptor with Spring Boot”

    1. Hi Sofiane,

      I had done the double check for you!
      Everything works fine! You can download attached sourcecode for testing.

      New Logs from my test:

      -> Should double check you environment:

      – Java 1.8
      – Maven 3.3.9
      – Spring Tool Suite – Version 3.8.1.RELEASE
      – Spring Boot: 1.4.3.RELEASE

      –> Spring Boot: 1.5.4.RELEASE(latest version up to now) is fine.

      Regards,
      JSA

Got Something To Say:

Your email address will not be published. Required fields are marked *

*