在Jersey中使用名称绑定注释 [英] Using name binding annotations in Jersey

查看:70
本文介绍了在Jersey中使用名称绑定注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@NameBinding 注释如何在Jersey中对特定资源方法或资源类应用过滤器?



考虑以下注释:

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

它是如何工作的?

解决方案

名称绑定



名称绑定是一个概念,它允许向JAX-RS运行时说明仅针对特定资源方法执行特定过滤器或拦截器。当过滤器或拦截器仅限于特定的资源方法时,我们说它是名称绑定的。没有这种限制的过滤器和拦截器称为 global



定义名称绑定注释



可以使用 @NameBinding 注释。此批注用作应用于提供者和资源方法的其他用户实现的批注的元标注。请参阅以下示例:

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

上面的示例定义了一个新的 @Compress 注释,它是一个名称绑定注释,因为它是用 @NameBinding @Compress 注释可用于将过滤器和拦截器绑定到端点。



将过滤器或拦截器绑定到端点



假设您有一个执行GZIP压缩的拦截器,并且您希望将此类拦截器绑定到资源方法。为此,请注释资源方法和拦截器,如下所示:

  @Compress 
公共类GZIPWriterInterceptor实现WriterInterceptor {

@Override
public void aroundWriteTo(WriterInterceptorContext context)
throws IOException,WebApplicationException {
final OutputStream outputStream = context.getOutputStream ();
context.setOutputStream(new GZIPOutputStream(outputStream));
context.proceed();
}
}



  @Path(helloworld)
公共类HelloWorldResource {

@GET
@Produces(text / plain)
public String getHello (){
返回Hello World!;
}

@GET
@Path(太多数据)
@Compress
public String getVeryLongString(){
String str = ... //非常长的字符串
return str;
}
}

@Compress 应用于资源方法 getVeryLongString()和拦截器 GZIPWriterInterceptor 。只有在执行具有此类注释的任何资源方法时,才会执行拦截器。



在上面的示例中,拦截器仅针对 getVeryLongString()方法执行。对于方法 getHello(),不会执行拦截器。在这个例子中,原因可能很清楚。我们只想压缩长数据,我们不需要压缩Hello World!的短响应。



可以在资源类上应用名称绑定。在示例中, HelloWorldResource 将使用 @Compress 进行注释。这意味着在这种情况下所有资源方法都将使用压缩。



请注意,全局过滤器始终执行,因此即使对于具有任何名称绑定注释的资源方法也是如此。 / p>

文档





示例




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

Consider the following annotation:

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

How does it work?

解决方案

Name binding

Name binding is a concept that allows to say to a JAX-RS runtime that a specific filter or interceptor will be executed only for a specific resource method. When a filter or an interceptor is limited only to a specific resource method we say that it is name-bound. Filters and interceptors that do not have such a limitation are called global.

Defining a name binding annotation

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

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

The example above defines a new @Compress annotation which is a name binding annotation as it is annotated with @NameBinding. The @Compress annotation can be used to bind filters and interceptor to endpoints.

Binding a filter or interceptor to an endpoint

Consider you have an interceptor that performs GZIP compression and you want to bind such interceptor to a resource method. To do it, annotate both the resource method and the interceptor, as following:

@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;
    }
}

The @Compress is applied on the resource method getVeryLongString() and on the interceptor GZIPWriterInterceptor. The interceptor will be executed only if any resource method with such a annotation will be executed.

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

Name binding can be applied on a resource class. In the example HelloWorldResource would be annotated with @Compress. This would mean that all resource methods will use compression in this case.

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

Documentation

Examples

这篇关于在Jersey中使用名称绑定注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆