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:
The mapping of the URI path space is presented in the following table:
URI path | Resource class | HTTP methods | Allowed values |
---|---|---|---|
/projects/{id} | ProjectsResource | GET | int |
/projects | ProjectsResource | GET | N/A |
/projects/detailed/{id} | ProjectsResource | GET | int |
/projects/detailed | ProjectsResource | GET | N/A |
/tasks/{id}?detailed={true|false} | TasksResource | GET | int, boolean |
/tasks | TasksResource | GET | N/A |
/tasks/detailed | TasksResource | GET | N/A |
/users/{id}?detailed={true|false} | UsersResource | GET | int, boolean |
/tasks?detailed={true|false} | UsersResource | GET | N/A |
Application is based on Grizzly container (see App
). Everything needed (resources/providers) is registered in EntityFilteringApplication
.
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 EPL+GPL license) JAX-RS 2.1 (JSR 370) 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 EPL+GPL license) JAX-RS 2.1 (JSR 370) 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" } ] }
Run the example as follows:
mvn clean package exec:java
This deploys current example using Grizzly. You can access the application at:
This examples uses by default Entity Data Filtering feature together with MOXy. To switch MOXy JSON provider to Jackson (2.x) JSON provider simply
register(new MoxyJsonConfig().setFormattedOutput(true).resolver())
register(JacksonFeature.class)
in EntityFilteringApplication
class.