Using Spring MVC HandlerInterceptor with Spring Boot - Hello World example | JavaInUse

Using Spring MVC HandlerInterceptor with Spring Boot - Hello World example

In this post we develop a simple Spring Boot application with interceptors.
We use the interceptor to log the user activity. We will expose an API and then log the status of User interaction.


This tutorial is explained in the below Youtube Video.

Lets Begin-

The project will be as follows-

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""







Next we define the LoggerInterceptor which implements the HandlerInterceptor.
The LoggerInterceptor will override the following methods-
  • preHandle() – This method is used to intercept the request before it’s handed over to the handler method. If It turns a boolean value, “true” : continue the handler execution chain; “false”, stop the execution chain and return it.
  • postHandle() – This method is used to intercept the request after the handler execution, Here user can manipulate the ModelAndView object before render it to view page.
  • afterCompletion() – This is a HandlerInterceptor callback method called after the complete request has finished.

Define the class implementing the spring HandlerInterceptor interface as follows-
package com.javainuse.interceptor.config;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class LoggerInterceptor implements HandlerInterceptor {

	Logger log = org.slf4j.LoggerFactory.getLogger(this.getClass());

	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object object, Exception arg3)
			throws Exception {"Request is complete");

	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object object, ModelAndView model)
			throws Exception {"Handler execution is complete");

	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object object) throws Exception {"Before Handler execution");
		return true;


Next we register the custom loggerInterceptor using the Spring WebMvcConfigurerAdapter class.
package com.javainuse.interceptor.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

public class InterceptorConfig extends WebMvcConfigurerAdapter {

	LoggerInterceptor logInterceptor;

	public void addInterceptors(InterceptorRegistry registry) {

Finally we expose the LoggerController as follows-
package com.javainuse.interceptor.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

public class LoggerController {

	Logger log = LoggerFactory.getLogger(this.getClass());

	public String executeLogger() {"inside the executeLogger method");
		return "Hello World Logger Interceptor";

Finally create the SpringBoot class annotated with @SpringBootApplication.
package com.javainuse.interceptor.config;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class SpringBootHelloWorldApplication {

	public static void main(String[] args) {, args);

After running the application- go to http://localhost:8080/logger
In the console we can see the request gets intercepted as follows-

Download Source Code

Download it -
Spring Boot + Interceptors Hello World Example

See Also

Spring Boot Hello World Application- Create simple controller and jsp view using Maven Spring Boot Tutorial-Spring Data JPA Spring Boot + Simple Security Configuration Pagination using Spring Boot Simple Example Spring Boot + ActiveMQ Hello world Example Spring Boot + Swagger Example Hello World Example Spring Boot + Swagger- Understanding the various Swagger Annotations Spring Boot Main Menu Spring Boot Interview Questions