JAX-RS Filters

by Vibrant Publishers


JAX-RS is the Java implementation for REST services.There are a lot of features in this library that helps us to build web services in Java. The JAX-RS Filters are used for processing the incoming and outgoing requests and responses including headers, entity and other parameters. This feature was introduced in JAX-RS 2.0. These filters get executed after a request and before a response.The filters are available for both server and client side processing.

Use of filters

You might be wondering what is the use of filters in web services. Well, there can be situations where you would like to do a common modification for all the requests originating from a particular client or some signature like “Powered by XX” in all the responses going from a particular server. Instead of doing this implementation on all the service methods we would use a filter which will include this modification in all the API requests and responses.

The below given is a diagram that schematically represents the filtering process on both client and server sides.



In the first section we got to know that there are filters available for server side and client side as well. Let us look into the details of their types and implementations.

Server Side Filters

The server side has got two types of filters.

  • ContainerRequestFilters

            They get executed before your JAX-RS request resource method is getting invoked.This filter can be used in two ways post-matching and pre-matching. In post-matching filters will be applied after a suitable resource method selection that processes the actual request. Here the filter will not affect the resource matching process. In certain scenarios, this can become a disadvantage. In-order to solve that problem there is pre-matching. When we mark a server request filter as pre-matching using the @PreMatching annotation, the filters are executed before the request matching process.

  • ContainerResponseFilters

       These filters get executed after your response method is invoked.They will help you to alter the outgoing response before it reaches the client. ContainerResponseFilter can only be implemented in the pre-matching way. With this filter one can modify the HTTP headers, media type(json,xml,etc.) and other meta-data. They also have the capability to abort a method execution.

Client Side Filters

Like we saw on the server side, the client side also has two types of filters.

  • ClientRequestFilters

           These filters are executed before the client API request is sent to the server over a network.

  • ClientResponseFilters

             These filters will be invoked after the client receives a response from the server, but before it is being unmarshalled. They can modify the response body before it is getting processed by the client side code.


JAX-RS filters are useful in dealing with modification of REST API request-response services.These filters can be installed at any point in the REST service pipeline. There can be multiple request-response filters where they are executed in a chain. It is very useful when we are working with API driven applications where we might need modification on multiple REST APIs at a given time.