Download Jersey 2.10 User Guide

Transcript
Entity Data Filtering
• Entity-filtering annotations identifying the applied scopes attached to a resource method or class (serverside only)
When the multiple approaches are combined, the priorities of calculating the applied scopes are as follows:
Entity annotations in request or response > Property passed through
Configuration > Annotations applied to a resource method or class.
In a graph of domain model objects, the entity-filtering scopes are applied to the root node as well as
transitively to all the child nodes. Fields and child nodes that do not match at least a single active scope are
filtered out. When the scope matching is performed, annotations applied to the domain model classes and
fields are used to compute the scope for each particular component of the model. If there are no annotations
on the class or it's fields, the default scope is assumed. During the filtering, first, the annotations on root
model class and it's fields are considered. For all composite fields that have not been filtered out, the
annotations on the referenced child class and it's fields are considered next, and so on.
18.3.1. Server-side Entity Filtering
To pass entity-filtering annotations via Response [http://jax-rs-spec.java.net/nonav/2.0/apidocs/
javax/ws/rs/core/Response.html] returned from a resource method you can leverage the
Response.ResponseBuilder#entity(Object, Annotation[]) [http://jax-rs-spec.java.net/nonav/2.0/apidocs/
javax/ws/rs/core/Response.ResponseBuilder.html#entity(java.lang.Object,
java.lang.annotation.Annotation[])] method. The next example illustrates this approach. You will also
see why every custom entity-filtering annotation should contain a factory for creating instances of the
annotation.
Example 18.13. ProjectsResource - Response entity-filtering annotations
1
2
3
4
5
6
7
8
9
10
11
12
13
@Path("projects")
@Produces("application/json")
public class ProjectsResource {
@GET
public Response getProjects(@QueryParam("detailed") final boolean isDetaile
return Response
.ok()
.entity(new GenericEntity<List<Project>>(EntityStore.getProject
isDetailed ? new Annotation[]{ProjectDetailedView.Facto
.build();
}
}
Annotating a resource method / class is typically easier although it is less flexible and may require more
resource methods to be created to cover all the alternative use case scenarios. For example:
219