Using name binding annotations in Jersey

How does the @NameBinding annotation work in Jersey to apply a filter to specific resource methods or a resource class?

Consider the following annotation:

 @NameBinding @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD, ElementType.TYPE}) public @interface SomeAnnotaion{} 

How it works?

+3
java rest jersey
source share
1 answer

name binding

Name binding is a concept that allows you to tell at JAX-RS runtime that a particular filter or interceptor will only be executed for a particular resource method. When a filter or interceptor is limited only to a specific resource method, we say that it is associated with a name. Filters and interceptors that do not have this restriction are called global.

Name Binding Annotation Definition

Filters or interceptors can be assigned to a resource method using the @NameBinding annotation. This annotation is used as a meta annotation for other custom annotations that apply to providers and resource methods. See the following example:

 @NameBinding @Retention(RetentionPolicy.RUNTIME) public @interface Compress {} 

The above example describes a new @Compress annotation, which is a name binding annotation because it is annotated with @NameBinding . The @Compress can be used to bind filters and interceptors to endpoints.

Link a filter or interceptor to an endpoint

You have an interceptor that performs GZIP compression, and you want to associate such an interceptor with a resource method. To do this, annotate both the resource method and the interceptor, as shown below:

 @Compress public class GZIPWriterInterceptor implements WriterInterceptor { @Override public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException { final OutputStream outputStream = context.getOutputStream(); context.setOutputStream(new GZIPOutputStream(outputStream)); context.proceed(); } } 
 @Path("helloworld") public class HelloWorldResource { @GET @Produces("text/plain") public String getHello() { return "Hello World!"; } @GET @Path("too-much-data") @Compress public String getVeryLongString() { String str = ... // very long string return str; } } 

@Compress applies to the getVeryLongString() resource method and to the GZIPWriterInterceptor interceptor. An interceptor will only be executed if any resource method with this annotation is executed.

In the above example, the interceptor will only execute for the getVeryLongString() method. The interceptor will not execute for the getHello() method. In this example, the reason is probably obvious. We would like to compress only long data, and we do not need to compress the short answer "Hello World!" .

Name binding can be applied to a resource class. In the example, HelloWorldResource will be annotated using @Compress . This means that all resource methods will use compression in this case.

Note that global filters are always executed, so even for resource methods that have any name binding annotations.

Documentation

Examples

  • Record HTTP Requests and Responses Using Filters and Name Binding Annotations
  • Token-based authentication using filters and name binding annotations
  • Using name binding hooks and annotations to add a property to JSON with Jackson
  • Name-referenced filters for security purposes
+13
source share

All Articles