How to create an interceptor in Quarkus?
Natan Ferreira- 0
- 838
When developing REST APIs, it’s common to want to intercept requests and responses to log information, validate data, or even apply business rules and security checks. In JAX-RS-based applications, like those using Quarkus, this is possible using request and response filters.
In this post, we’ll explore how to implement a simple interceptor in Quarkus that allows us to observe and manipulate the HTTP request/response lifecycle transparently.
Hands on

The example below demonstrates a filter that intercepts incoming HTTP requests and outgoing responses. We use the @Provider annotation to register the filter in the Quarkus context:
import jakarta.ws.rs.container.ContainerRequestContext;
import jakarta.ws.rs.container.ContainerRequestFilter;
import jakarta.ws.rs.container.ContainerResponseContext;
import jakarta.ws.rs.container.ContainerResponseFilter;
import jakarta.ws.rs.ext.Provider;
import java.io.IOException;
@Provider
public class InterceptorFilter implements ContainerRequestFilter, ContainerResponseFilter {
@Override
public void filter(ContainerRequestContext containerRequestContext) throws IOException {
System.out.println(">>> Base URI: " + containerRequestContext.getUriInfo().getBaseUri());
System.out.println(">>> Request Headers: " + containerRequestContext.getHeaders().values());
}
@Override
public void filter(ContainerRequestContext containerRequestContext, ContainerResponseContext containerResponseContext) throws IOException {
System.out.println(">>> Response Status: " + containerResponseContext.getStatus());
if (containerResponseContext.getEntity() != null) {
System.out.println(">>> Response Body: " + containerResponseContext.getEntity().toString());
}
}
}
✅ Key points:
@Provider: Registers the class as a JAX-RS component that is automatically discovered.ContainerRequestFilter: Interface used to intercept requests before they reach the endpoint.ContainerResponseFilter: Interface used to intercept responses before they are sent to the client.
With this, you can:
- Log HTTP headers.
- Add custom response headers.
- Block access to routes (e.g., authentication/authorization logic).
- Modify the response body if needed.
Conclusion
Interceptors like the one we implemented here are powerful tools for observing or modifying the request/response lifecycle in RESTful APIs with Quarkus. By using @Provider and implementing ContainerRequestFilter and ContainerResponseFilter, you can create reusable and centralized logic for common request/response handling.
This approach improves the maintainability, observability, and security of your application.
If you want to take this further, you could integrate it with tools like OpenTelemetry, JWT validation, tracing, or store logs for audit purposes.
Author
-
I am a seasoned Full Stack Software Developer with 8+ years of experience, including 6+ years specializing in Java with Spring and Quarkus. My core expertise lies in developing robust RESTful APIs integrated with Cosmos Db, MySQL, and cloud platforms like Azure and AWS. I have extensive experience designing and implementing microservices architectures, ensuring performance and reliability for high-traffic systems. In addition to backend development, I have experience with Angular to build user-friendly interfaces, leveraging my postgraduate degree in frontend web development to deliver seamless and responsive user experiences. My dedication to clean and secure code led me to present best practices to my company and clients, using tools like Sonar to ensure code quality and security. I am a critical thinker, problem solver, and team player, thriving in collaborative environments while tackling complex challenges. Beyond development, I share knowledge through my blog, NatanCode, where I write about Java, Spring, Quarkus, databases, and frontend development. My passion for learning and delivering innovative solutions drives me to excel in every project I undertake.
View all posts