在 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-bound.没有这种限制的过滤器和拦截器称为全局.
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.
@NameBinding
注释文件- 关于过滤器和拦截器的泽西岛文档
- 使用过滤器和名称绑定注释记录 HTTP 请求和响应
- 使用过滤器和名称绑定注释的基于令牌的身份验证
- 使用拦截器和名称绑定注释将属性添加到 JSON 与 Jackson
- 出于安全目的使用名称绑定的过滤器
相关文章