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() 
	{
		packages("com.howtodoinjava.jersey");
		// 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.
		register(JacksonFeature.class);
		
		register(SelectableEntityFilteringFeature.class);
	    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:

<init-param>
    <param-name>jersey.config.server.provider.classnames</param-name>
    <param-value>
        org.glassfish.jersey.filter.LoggingFilter,
        org.glassfish.jersey.message.filtering.SelectableEntityFilteringFeature,
        org.glassfish.jersey.jackson.JacksonFeature
    </param-value>
</init-param>

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

<init-param>
    <param-name>jersey.config.entityFiltering.selectable.query</param-name>
    <param-value>select</param-value>
</init-param>

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

Test SelectableEntityFilteringFeature

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

    Response:

    {"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

    Response:

    {"employeeList":[{"id":1},{"id":2},{"id":3}]}
  3. Hit HTTP GET: http://localhost:8080/JerseyDemos/rest/employees/1

    Response:

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

    Response:

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

    Response:

    {"name":"Lokesh Gupta"}

It’s easy to implement and highly useful.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

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.

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.