This example demonstrates how to define custom entity filtering annotations (views) and how to apply them on domain classes as well as on JAX-RS resource classes or JAX-RS resource methods.

The full description how Entity Data Filtering can be found in Jersey User Guide, chapter Entity Data Filtering. Sections relevant to this example (describing this exact example) are:

Contents

The mapping of the URI path space is presented in the following table:

URI path Resource class HTTP methods Allowed values Notes
/projects/{id} ProjectsResource GET int Basic view on Project class - id, name, description
/projects ProjectsResource GET N/A Basic view on Project class - id, name, description
/projects/detailed/{id} ProjectsResource GET int Detailed view on Project class - id, name, description, tasks, users
/projects/detailed ProjectsResource GET N/A Detailed view on Project class - id, name, description, tasks, users
/tasks/{id}?detailed={true|false} TasksResource GET int, boolean Basic / Detailed view on Task class - id, name, description, (project, users)
/tasks TasksResource GET N/A Basic view on Task class - id, name, description
/tasks/detailed TasksResource GET N/A Detailed view on Task class - id, name, description, project, users
/users/{id}?detailed={true|false} UsersResource GET int, boolean Basic / Detailed view on User class - id, name, email, (projects, tasks)
/tasks?detailed={true|false} UsersResource GET N/A Basic / Detailed view on User class - id, name, email, (projects, tasks)

Application is based on Grizzly container (see App). Everything needed (resources/providers) is registered in EntityFilteringApplication.

Sample Response

Even though the same instance of, e.g. Project class, is used to create response for both basic and detailed view the actual data sent over the wire differ for each of these two views. For basic view it looks like:

{
   "description" : "Jersey is the open source (under dual CDDL+GPL license) JAX-RS 2.0 (JSR 339) production quality Reference Implementation for building RESTful Web services.",
   "id" : 1,
   "name" : "Jersey"
}
And for detailed view it looks like:
{
   "description" : "Jersey is the open source (under dual CDDL+GPL license) JAX-RS 2.0 (JSR 339) production quality Reference Implementation for building RESTful Web services.",
   "id" : 1,
   "name" : "Jersey",
   "tasks" : [ {
      "description" : "Entity Data Filtering",
      "id" : 1,
      "name" : "ENT_FLT"
   }, {
      "description" : "OAuth 1 + 2",
      "id" : 2,
      "name" : "OAUTH"
   } ],
   "users" : [ {
      "email" : "very@secret.com",
      "id" : 1,
      "name" : "Jersey Robot"
   } ]
}

Running the Example

Run the example as follows:

mvn clean package exec:java

This deploys current example using Grizzly. You can access the application at:

Using Jackson instead of MOXy

This examples uses by default Entity Data Filtering feature together with MOXy. To switch MOXy JSON provider to Jackson (2.x) JSON provider simply

  • comment registration of MOXy ContextResolver, and
    register(new MoxyJsonConfig().setFormattedOutput(true).resolver())
  • uncomment registration of JacksonFeature
    register(JacksonFeature.class)
in EntityFilteringApplication class.