Skip to main content

Posts

Showing posts with the label Spring REST

πŸ’₯ “Filter vs Interceptor in Spring — Who’s the Real Gatekeeper?”

πŸ’₯ “Filter vs Interceptor in Spring — Who’s the Real Gatekeeper?” πŸ” The Hidden Guards of Your REST APIs (with Real Examples, Jokes & Weird Discoveries!) 🎯 1) What Is Filter / Interceptor / OncePerRequestFilter? 🧹 Filter A Filter is a component from the Servlet API that sits at the very beginning of a request's journey. It is invoked before the Spring context even gets involved. It's great for tasks like CORS configuration, encoding setup, basic logging, and blocking or modifying raw requests/responses. 🎀 Think of Filter as the club bouncer who checks everyone at the entrance—before they even see the dance floor! πŸ•΅️ Interceptor A HandlerInterceptor is a Spring MVC feature that works after the request has passed through the DispatcherServlet but before the controller method is executed. It can inspect the handler (controller) method, add attributes, or block the request if needed. πŸ€“ It’s like the guy inside the club checking if you’re VIP before letting you in ...