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

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

问题描述

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

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

考虑以下注释:

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

它是如何工作的?

推荐答案

名称绑定

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

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.

过滤器或拦截器可以使用 @NameBinding 注释.此注释用作其他用户实现的注释的元注释,这些注释应用于提供者和资源方法.请参见以下示例:

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 {}

上面的例子定义了一个新的 @Compress 注释,它是一个名称绑定注释,因为它是用 @NameBinding.@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.

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

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

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

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.

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

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!".

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

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.

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

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