Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Binding Strategies for JAX-RS Filters and Interceptors

DZone's Guide to

Binding Strategies for JAX-RS Filters and Interceptors

Target JAX-RS filters and interceptors with named and dynamic binding.

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

JAX-RS 2.0 defines multiple ways that 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 an 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):

// Step 1: Define a custom annotation with the @NamedBinding annotation
@NameBinding
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(value = RetentionPolicy.RUNTIME)
public @interface Audited { }

// Step 2: Apply the custom annotation on the filter or interceptor
@Provider
@Audited
public class AuditFilter implements ContainerRequestFilter {
//filter implementation....
}

// Step 3: Apply the same annotation to the required resource class or method
@GET
@Path("{id}")
@Produces("application/json")
@Audited
public Response find(@PathParam("id") String custId){
//search and return customer info
} 

Note: If 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 most static way of binding made possible using @NamedBinding:

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 us to register the filter or interceptor once the resource method has been selected.

For more on JAX-RS 2.0…

Cheers!

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.

Topics:
java ,javaee ,jax rs

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}