java 在 Jersey 中使用名称绑定注释
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38520881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using name binding annotations in Jersey
提问by chaitanya
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?
它是如何工作的?
回答by cassiomolin
Name binding
名称绑定
Name bindingis 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.
名称绑定是一个概念,它允许对 JAX-RS 运行时说特定的过滤器或拦截器将仅针对特定的资源方法执行。当过滤器或拦截器仅限于特定的资源方法时,我们说它是名称绑定的。没有这种限制的过滤器和拦截器称为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
注释将过滤器或拦截器分配给资源方法。此注释用作其他用户实现的注释的元注释,这些注释应用于提供者和资源方法。请参阅以下示例:
@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.
上面的例子定义了一个新的@Compress
注解,它是一个名称绑定注解,因为它用@NameBinding
. 该@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. To do it, annotate both the resource method and the interceptor, as following:
假设您有一个执行 GZIP 压缩的拦截器,并且您希望将此类拦截器绑定到资源方法。为此,请同时注释资源方法和拦截器,如下所示:
@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.
该@Compress
应用的资源的方法getVeryLongString()
和拦截器GZIPWriterInterceptor
。只有在执行任何具有此类注释的资源方法时才会执行拦截器。
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!"
.
在上面的例子中,拦截器将只为getVeryLongString()
方法执行。不会为 method 执行拦截器getHello()
。在这个例子中,原因可能很清楚。我们只想压缩长数据,我们不需要压缩 的短响应"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.
名称绑定可以应用于资源类。在示例HelloWorldResource
中将用@Compress
. 这意味着在这种情况下所有资源方法都将使用压缩。
Note that global filters are executed always, so even for resource methods which have any name binding annotations.
请注意,始终执行全局过滤器,因此即使对于具有任何名称绑定注释的资源方法也是如此。