简体   繁体   English

在Jersey中使用名称绑定注释

[英]Using name binding annotations in Jersey

How does the @NameBinding annotation work in Jersey to apply a filter on particular resource methods or resource class? @NameBinding注释如何在Jersey中对特定资源方法或资源类应用过滤器?

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. 名称绑定是一种概念,它允许向JAX-RS运行时说明仅针对特定资源方法执行特定过滤器或拦截器。 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. 可以使用@NameBinding批注将过滤器或拦截器分配给资源方法。 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 . 上面的示例定义了一个新的@Compress注释,它是一个名称绑定注释,因为它是使用@NameBinding注释的。 The @Compress annotation can be used to bind filters and interceptor to endpoints. @Compress注释可用于将过滤器和拦截器绑定到端点。

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. 假设您有一个执行GZIP压缩的拦截器,并且您希望将此类拦截器绑定到资源方法。 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 . @Compress应用于资源方法getVeryLongString()和拦截器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. 在上面的示例中,拦截器仅针对getVeryLongString()方法执行。 The interceptor will not be executed for method getHello() . 不会为方法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!" 我们想只压缩长数据,我们不需要压缩"Hello World!"的简短响应"Hello World!" .

Name binding can be applied on a resource class. 名称绑定可以应用于资源类。 In the example HelloWorldResource would be annotated with @Compress . 在示例中, HelloWorldResource将使用@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 例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM