In this tutorial, we will learn how to leverage Java Spring’s handler interceptors to be able to intercept HTTP requests and perform pre and post processing.
MVC handler interceptors let you execute code before and after processing your HTTP requests. Let’s go over some scenarios where the pre and post-processing on handler interceptors could help with:
1 — Capture metrics for your HTTP requests processing times
2 — Add logs for every one of your HTTP requests
3 — Validate routes contain the right authentication header
Let’s go first over the setup required for creating your own handler interceptor. Any time you create a handler interceptor there will be two key components involved:
1 — The handler interceptor class
2 — Adding bean definitions for your handler interceptor on your main class