This example demonstrates how to use entity filtering feature dynamically with defining what should be sent to the other side based on query parameters and how to apply the rules 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 | Notes |
---|---|---|---|---|
/people/{id}?select={select} | PersonResource | GET | id = any value, select = field names from Person/Address/PhoneNumber class | Returns fields of Person/Address/PhoneNumber |
/people/{id}/addresses?select={select} | PersonResource | GET | id = any value, select = field names from Address/PhoneNumber class | Returns fields of Address/PhoneNumber |
Application is based on Grizzly container (see App
). Everything needed (resources/providers) is registered in SelectableEntityFilteringApplication
.
Even though the same instance of, e.g. Person class, is used to create response the given values of select
query parameter are used to select the fields that would be transferred over the wire. For people/1234?select=familyName,givenName
it looks like:
{ "familyName": "Dowd", "givenName": "Andrew" }
And for people/1234?select=familyName,givenName,addresses.phoneNumber.number
it looks like:
{ "addresses":[ { "phoneNumber":{ "number": "867-5309" } } ], "familyName": "Dowd", "givenName": "Andrew" }
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 SelectableEntityFilteringApplication
class.