Annotation Interface FilterContent


@Target({METHOD,TYPE,PACKAGE}) @Retention(RUNTIME) public @interface FilterContent
Declares that a method should be invoked to filter the visibility of an object for a specific User.

Filtering allows objects to dynamically adjust their exposed content based on who is viewing them. This is especially useful in projections, document models, or search results that may be shared with different roles.

Injection: The annotated method may accept the following parameters:

  • User – the current user performing the access
  • Root object – the top-level object being filtered (useful for context when filtering nested values)
Any other arguments will be ignored.

Return value: The method should return:

  • this – if the value is fully visible
  • a modified copy – if only a subset of the value should be shown
  • null – if the value should be completely hidden

Recursive filtering: Filtering is automatically applied to nested objects, collections, and maps. When filtering results in null for an item inside a collection or map:

  • The item is removed from a List
  • The key-value pair is removed from a Map

Example (filtering the object):

public class Order {
    @FilterContent
    Order filter(User user) {
        return user.hasRole("admin") ? this : new Order(maskedFieldsOnly());
    }
}

Example (filtering a nested item with root injection):

@FilterContent
public LineItem filter(User user, Order root) {
    return root.isPublic() ? this : null;
}

Invocation: Filtering can be applied in two ways:

For automatic filtering: @FilterContent can be applied to a handler at three levels:

  • Method level - filters specific method results
  • Type level - automatically filters all handler results in the class
  • Package level - automatically filters all handler results in the package or subpackage
See Also: