SpringBootApplication

Execute the following SpringBootApplication code:

Java




package com.w3wiki.requestinterceptorexample;
  
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
  
@SpringBootApplication
public class RequestInterceptorExampleApplication {
  
    public static void main(String[] args) {
        SpringApplication.run(RequestInterceptorExampleApplication.class, args);
    }
}


Step-1: Test the response on Postman or any browser by going to – “default port/api/students”

Output window of testing REST API using PostMan

PostMan Output

Step-2: Check the console for preHandle, postHandle, and afterCompletion Method Outputs:

Console output of the interceptor methods

Console Output



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.

Similar Reads

Limitations

Performance overhead: If our application deals with a large number of interceptors, then it is not only complex to manage each one of them but also it results in reduced performance. Disconnected with global state: Interceptors do not store the state of any application as to what data is currently updated & where. The interceptor must be implemented in a way that its interception doesn’t affect other parts of our application & should only impact the request for its intercepting. Security: When an interceptor logs the request & response data it must be doing so in a protective manner else it would expose the request’s data to attackers....

Advantages

Reusability: The interceptors we implement in our application, can be reused back & forth for handling a request & response to multiple requests. They also help us minimize the code in the controller, shifting the authentication, and modification logic from the controller to the Interceptor. Logging & Monitoring: Interceptors help us log, debug & monitor incoming & outgoing requests. This saves time, to apprehend the state of request back and forth. Security and Authentication: Interceptors can verify the request made & delegate it as per the verification. Easy Configuration: Interceptors can be easily configured without affecting the whole application as the primary purpose of the interceptor is just to intercept requests & responses....

Step-by-Step Implementation

Here’s a step-by-step implementation for implementing an Interceptor in Spring Boot....

1. POM File

We have included the following dependencies in our POM File:...

2. Entity Class

...

3. Configuration Class

Below is our entity class, it contains 2 annotations:-...

4. Request Interceptor

...

5. REST Controller

This is a Java class annotated with @Configuration for auto-configuration that is, it indicates this class contains configuration for the spring application context. Basically, it indicates that this class will contain some beans that must be registered with the application context to be used in the application....

6. SpringBootApplication

...

Contact Us