07.Filters and Interceptors - JohnyzHub/jax-rs GitHub Wiki
Filters can be divided into two categories into to in Jax-rs specification namely
- Client-side Filters
- Server-side filters
There are two filters those are applicable at client side.
-
ClientRequestFilter
This filter is useful in filtering the outgoing request from the client. For the working example, refer MovieResourceClientRequestFilter.java -
ClientResponseFilter
This filter is useful in filtering the incoming response to the client. For the working example, refer MovieResourceClientResponseFilter.java
In order to activate the client side filters, these filters should be registered to the client.
There are three filters those are applicable at server side.
-
Pre matching ContainerRequestFilter
This filter is useful in filtering the incoming request, before finding the matching resource for the request. This class should be annotated with @PreMatching.
Notice that this filter gets executed if the base url(ie. localhost:8080/moviedirectory/rest) matches irrespective of the matching resource path (eg: directory). -
Post matching ContainerRequestFilter
This filter is useful in filtering the incoming request, after finding the matching resource for the request. All the server side filters are post matching by default. For example the authentication verification. Notice that this filter doesn't need special annotation such as @PreMatching in the previous one.For working example, refer MovieServiceRequestFilter.java
There is one more vital difference between prematching and postmatching request filters. If the client sends the request with invalid url, the prematching filter still gets executed where as the postmatching filter doesn't. That means, the prematching filters gets executed always.
-
ContainerResponseFilter
This filter is useful in filtering the outgoing response.
For working example, refer MovieServiceResponseFilter.java
- ReaderInterceptor applicable to the requests
-
WriterInterceptor
applicable to the responses
For working example, Please refer MovieServiceInterceptor.java
Notice that the interceptors always get executed after the filters if exists.
Execution Order:
Jax-rs specification also provides the functionality to
-
chain the filters (or) interceptors and setting the order of execution by using @Priority
The priority is specified with an Integer or Priorities. Priority sorts the filters and interceptors in ascending order for the requests and in descending order for the responses
-
Set the filters or interceptors applicable only for specific resources.
- @NameBinding: In short, create a custom annotation and annotate it with @NameBinding. Place this custom annotation on the filter (or) interceptor and the rest resource (class or method level). While doing so, filter or interceptor classes do not need to be annotated with @Provider. Jax-rs container activates this functionality during runtime. This is an easy and clean way of applying filter/interceptor to a set of resources. One caveat is in order to verify all the resources a filter is applied to, needs a search in the whole workspace.
- Dynamically Binding: In short, implement the interface DynamicFeature and override the method to register the filter or interceptor to the FeatureContext based on the ResourceInfo. Jax-rs container activates this functionality during application deployment. This is helpful if you would like to declare a set of resources at a single place to easily identify the filter/interceptor applied to them. One caveat is the class may get bloated with increase in number of resources.
Please refer below resources for more information regarding the jax-rs resource life cycle
- Filters and Interceptors by baeldung
- Understanding the JAX-RS resource life cycle section in the book Restful java web services