POM File
We have included the following dependencies in our POM File:
- Spring-Boot-starter-web
- Lombok Library
Note: Lombok Library is completely OPTIONAL, you can write your own setters & getters. We’re using it for simplicity.
XML
<? xml version = "1.0" encoding = "UTF-8" ?> < project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion >4.0.0</ modelVersion > < parent > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-parent</ artifactId > < version >3.1.5</ version > < relativePath /> <!-- lookup parent from repository --> </ parent > < groupId >com.w3wiki</ groupId > < artifactId >requestInterceptor</ artifactId > < version >0.0.1-SNAPSHOT</ version > < name >requestInterceptor</ name > < description >Request Interceptor Demo</ description > < properties > < java.version >17</ java.version > </ properties > < dependencies > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-web</ artifactId > </ dependency > < dependency > < groupId >org.projectlombok</ groupId > < artifactId >lombok</ artifactId > < optional >true</ optional > </ dependency > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-test</ artifactId > < scope >test</ scope > </ dependency > </ dependencies > < build > < plugins > < plugin > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-maven-plugin</ artifactId > < configuration > < excludes > < exclude > < groupId >org.projectlombok</ groupId > < artifactId >lombok</ artifactId > </ exclude > </ excludes > </ configuration > </ plugin > </ plugins > </ build > </ project > |
Spring Boot – Interceptor
Spring Boot Interceptor is an additional component that will intercept every request and response dispatch and perform some operations on it.
Interceptors in web applications play a major in performing initial or finalization work.
Interceptors have one primary purpose – “To intercept an incoming or outgoing request”.
This intercepting behavior is done to perform some operation BEFORE or AFTER an action invocation. You can think of an interceptor as a mediator between the request and the business logic reserved for that request. Interceptor in Spring Boot can be implemented either by extending the HandlerInterceptorAdapter class or by implementing HandlerInterceptor Interface.
Interceptors are useful for many purposes some of which are listed below :
1. Logging
Logging is a common practice to understand the flow of code & debugging. Interceptors are widely for the same, Documenting or recording the requests made by the client or the responses sent back to them is generally useful in debugging and monitoring purposes.
2. Authentication
Authentication is a security mechanism that can be implemented inside an Interceptor before the request even reaches its appropriate controller. It can be intercepted by the Interceptor & authentication can be verified, if denied then the request doesn’t get delegated to the controller which not only minimizes the code but reduces a great overhead.
3. Request/Response Modification
Interceptors are also used for modifying requests & responses. It can be done in the following ways :
- Adding or modifying request parameters
- Modifying request headers, or response head
- Change status code
- Redirection to a different URL
- Handle custom error requests/responses which frees the controller or an addition of an external component just to handle the exceptions.