Jersey Entity Data Filter Example

Oftentimes, you want to make RESTful API consumers capable of filtering data that they need from a GET API, rather than forcing them to consume all the response data unnecessarily. There are two ways to achieve this filtering: server-side response filtering and client-side response filtering.

Server-side response filtering, though useful, requires us to change API source code 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 source code created for Jersey 2 Hello World Example.

1. Register SelectableEntityFilteringFeature

All you need to do is register SelectableEntityFilteringFeature in ResourceConfig class in this way. We are configuring the query parameter select to pass the filter parameters.

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.

2. Demo

Hit the URL: HTTP GET http://localhost:8080/JerseyDemos/rest/employees

API Response:

{
  "employeeList": [
    {
      "id": 1,
      "name": "Lokesh Gupta"
    },
    {
      "id": 2,
      "name": "Alex Kolenchiskey"
    },
    {
      "id": 3,
      "name": "David Kameron"
    }
  ]
}

Hit HTTP GET: http://localhost:8080/JerseyDemos/rest/employees?select=employeeList,id

API response:

{
  "employeeList": [
    {
      "id": 1
    },
    {
      "id": 2
    },
    {
      "id": 3
    }
  ]
}

Hit the URL: http://localhost:8080/JerseyDemos/rest/employees/1

API response:

{
  "id": 1,
  "name": "Lokesh Gupta"
}

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

API response;

{
  "name": "Lokesh Gupta"
}

Clearly, it is very easy to implement the client-side filtering in Jersey using the SelectableEntityFilteringFeature and it is highly useful.

Happy Learning !!

Leave a Reply

2 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

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

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

Our Blogs

REST API Tutorial