Java Reference
In-Depth Information
To manually bind a filter or interceptor to a resource method, the filter or interceptor class must be denoted
with an
@NameBinding
annotation. An
@NameBinding
annotation can be coded just like a standard annotation, but
the annotation implementation should also include the
@NameBinding
annotation in its interface. The following
annotation code could be used to create an
@NameBinding
annotation that might be placed on a filter that is
responsible for firing alerts:
@NameBinding
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(value = RetentionPolicy.RUNTIME)
public @interface Alerter { }
To associate the
@NameBinding
with a filter or interceptor, simply annotate the filter or interceptor class with it.
The following
AlertFilter
class is a filter implementation that is denoted with the
@Alerter
annotation:
@Provider
@Alerter
class AlertFilter implements ContainerRequestFilter,
ContainerResponseFilter {
...
}
That filter can now be bound to a resource method by annotating the resource method with the same
@
NameBinding
as the filter class, as demonstrated here:
@GET
@Produces("text/html")
@Alerter
public String getJobs(){
...
}
■
Note
this same concept can be applied to
Application
subclasses in order to globally bind the filter or interceptor.
Setting Priorities
As mentioned in the previous sections, filters and interceptors can be chained. Chains of filters or interceptors invoke
individual filters or interceptors based upon a given priority. To assign priority to a filter or interceptor, denote the
implementation class with the
@Priority
annotation. The
Priorities
class in JAX-RS is used to specify the type of
priority that needs to be set. There are built-in priorities for security, header decorators, decoders, and encoders.
In the following example, the priority of a filter, the
AlertFilter
that was introduced in the previous section, is set to
a priority of
Priorities.USER
:
@Provider
@Alerter
@Priority(Priorities.USER)