Jersey Selectable EntityFilteringFeature Example

Often times, you want to make RESTful API consumers capable of filtering data what they need from a GET API, rather than forcing them to consume all the response data unnecessary. There are two ways to achieve this filtering : server side response filtering and client side response filtering. Server side response filtering, though useful, requires you to change API sourcecode and configuration – but enabling client side response filtering does not require API level changes. It’s easy to implement and easier to use.

To demonstrate the use of client side response filtering using SelectableEntityFilteringFeature, I am using the sourcecode created for Jersey 2 Hello World Example.

Registering SelectableEntityFilteringFeature in Jersey

All you need to do is – register SelectableEntityFilteringFeature in ResourceConfig class in this way:

package com.howtodoinjava.jersey;

import org.glassfish.jersey.jackson.JacksonFeature;
import org.glassfish.jersey.message.filtering.SelectableEntityFilteringFeature;
import org.glassfish.jersey.server.ResourceConfig;

public class CustomApplication extends ResourceConfig 
	public CustomApplication() 
		// Configure MOXy Json provider. Comment this line to use Jackson. Uncomment to use MOXy.
        // register(new MoxyJsonConfig().setFormattedOutput(true).resolver());

        // Configure Jackson Json provider. Comment this line to use MOXy. Uncomment to use Jackson.
	    property(SelectableEntityFilteringFeature.QUERY_PARAM_NAME, "select");

If you want to enable this feature through web.xml file, then you can add the feature to the list of provider class names:


And for setting the query selector property, use the below code:


You are set to test the filtering. Let’s test it.

Test SelectableEntityFilteringFeature

  1. Hit HTTP GET: http://localhost:8080/JerseyDemos/rest/employees


    {"employeeList":[ {"id":1,"name":"Lokesh Gupta"}, {"id":2,"name":"Alex Kolenchiskey"}, {"id":3,"name":"David Kameron"}]}
  2. Hit HTTP GET: http://localhost:8080/JerseyDemos/rest/employees?select=employeeList,id


  3. Hit HTTP GET: http://localhost:8080/JerseyDemos/rest/employees/1


    {"id":1,"name":"Lokesh Gupta"}
  4. Hit HTTP GET: http://localhost:8080/JerseyDemos/rest/employees/1?select=id


  5. Hit HTTP GET: http://localhost:8080/JerseyDemos/rest/employees/1?select=name


    {"name":"Lokesh Gupta"}

It’s easy to implement and highly useful.

Happy Learning !!

Was this post helpful?

Join 8000+ Awesome Developers, Like YOU!

3 thoughts on “Jersey Selectable EntityFilteringFeature Example”

  1. Is it possible to use selectable filtering with application/xml response? When the client accepts application/json everything’s fine, but when I try to get xml response filtering stops working and I get full object fields.


Leave a Comment

About HowToDoInJava

This blog provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions, and frequently asked interview questions.