JAX-RS 2.0 defines multiple ways using which server side filters and interceptors can be bound to their target components.

  • Global Binding
  • Named Binding
  • Dynamic Binding

Global Binding

By default, JAX-RS filters and interceptors are bound to all the methods of resource classes in an application. That is, both request (pre and post) and response filters will be invoked whenever any resource method is invoked in response to a HTTP request by the client. This convention can be overridden using named binding or dynamic binding.

Named Binding

Filters and interceptors scoping can be handled in a fine-grained manner (based on per resource class/method)

https://gist.github.com/abhirockzz/5b4abe9716d9c772817d#file-applying_named_binding_jax_rs-txt

NoteIf it is applied to a class, the filter/interceptor will be bound to all its resource methods

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 made possible using@NamedBinding

https://gist.github.com/abhirockzz/212632b2ede349913594#file-dynamicfeature-java

The injected instance of the ResourceInfo interface helps you choose the resource method in dynamic fashion by exposing various methods and the FeatureContext interface allows us to register the filter or interceptor once the resource method has been selected.

https://gist.github.com/abhirockzz/e9528df2710ee4b0e778#file-authfilterdynamic-java

For more on JAX-RS 2.0 …

Cheers!

Binding Strategies for JAX-RS Filters and Interceptors

| Java Language| 431 views | 0 Comments
About The Author
- • Consultant - Identity & Access Management • Java language and platform fanatic

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>