Java Magazine, Jan/Feb 2016
ORACLE COM JAVAMAGAZINE JANUARY FEBRUARY 2016 36 web apps class or method If it is applied to a class the filter or interceptor will be bound to all the resource methods of the class @ GET @ Path id @ Produces application json @ Audited public Response find @ PathParam id String custId search and return customer info Dynamic binding JAX RS provides the DynamicFeature interface to help bind filters and interceptors dynamically at runtime They can be used in tandem with the more static way of binding which is made possible by using the @ NameBinding annotation just discussed public interface DynamicFeature public void configure ResourceInfo resInfo FeatureContext ctx The injected instance of the ResourceInfo interface helps you choose the resource method in a dynamic fashion by exposing various methods and the FeatureContext interface allows you to register the filter or interceptor once the resource method has been selected The following code shows an example @ Provider public class DynamicAuthFilterFeature implements DynamicFeature @ Override public void configure ResourceInfo resInfo FeatureContext ctx if UserResource class equals resInfo getResourceClass resInfo getResourceMethod getName contains PUT ctx register AuthenticationFilter class Binding and Registering Client Side Filters and Interceptors We saw that a class level @ Provider annotation is required for server side filters On the client side filters and interceptors are registered using ClientBuilder Client or WebTarget interfaces all of which implement the javax ws rs core Configurable interface which provides multiple overloaded versions of the register method Ordering Filters and Interceptors The @ Priority annotation can be used to define the order of execution of filters and interceptors It accepts a numerical value that is interpreted differently by request and response filters and interceptors The request filters ContainerRequestFilter and ClientRequestFilter and interceptors Reader Interceptor and WriterInterceptor are executed in ascending order of their priorities In other words the lesser valued @ Priority annotated providers are executed first The response filters ContainerResponseFilter and ClientResponseFilter are executed in exactly the opposite that is descending order In the absence of a @ Priority annotation a default value is assumed It is defined by the USER constant in javax ws rs Priorities this equals 5000 Priorities of client side components can be set using the register method of interfaces implementing Configurable
You must have JavaScript enabled to view digital editions.