Spring Boot - Interceptor - Example

Hello everyone, today we will develop a simple Spring Boot application with Spring MVC HandlerInterceptor.
Interceptors working with the HandlerMapping on the framework must implement the HandlerInterceptor interface. HandlerInterceptor is rudimentarily homogeneous to a Servlet Filter, but in contrast to the latter it just allows custom pre-processing with the option of prescribing the execution of the handler itself, and custom post-processing. This interface contains three main methods: 

 Let's begin,

Project Directory:


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
<relativePath />
<description>Demo project for Spring Boot</description>

Define the class implementing the spring HandlerInterceptor interface as follows-

package com.knf.dev.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class ServiceInterceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler)
throws Exception {

System.out.println("Pre Handle method is Calling,
request url" + " " + request.getRequestURI());
return true;

public void postHandle(HttpServletRequest request,
HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {

System.out.println("Post Handle method is Calling,
Response status" + " " + response.getStatus());

public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object handler,
Exception exception) throws Exception {

System.out.println("Request and Response is completed");

prehandle() – called before the genuine handler is executed, but the view is not engendered yet 
postHandle() – called after the handler is executed 
afterCompletion() – called after the consummate request has culminated and the view was 

Configuration of interceptors:

package com.knf.dev.interceptor;

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.WebMvcConfigurer;

public class ServiceInterceptorAppConfig implements WebMvcConfigurer {
ServiceInterceptor serviceInterceptor;

public void addInterceptors(InterceptorRegistry registry) {
// Custom interceptor, add intercept path and exclude intercept path


package com.knf.dev.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

public class HomeController {

public String getHello() {
return "you are done";

Finally, create the SpringBoot class annotated with @SpringBootApplication.

package com.knf.dev;

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

public class SpringbootInterceptorDemoApplication {

public static void main(String[] args) {
SpringApplication.run(SpringbootInterceptorDemoApplication.class, args);

After running the application- go to http://localhost:8080/hello

In the console, we can see the request gets intercepted as follows-
Pre Handle method is Calling, request url /hello
Post Handle method is Calling, Response status 200
Request and Response is completed

git clone:  

Popular posts from this blog

Spring boot video streaming example-HTML5

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Spring Boot + Mockito simple application with 100% code coverage

Spring Boot + OpenCSV Export Data to CSV Example

Custom Exception Handling in Quarkus REST API

DataTable-Pagination example with Spring boot, jQuery and ajax

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Node JS mini projects with source code - free download

Spring boot web project free download:User Registration System

Java - Blowfish Encryption and decryption Example