Java Reference
In-Depth Information
import
import
javax.ws.rs.container.DynamicFeature
javax.ws.rs.container.DynamicFeature
;
import
import
javax.ws.rs.container.ResourceInfo
javax.ws.rs.container.ResourceInfo
;
import
import
javax.ws.rs.core.FeatureContext
javax.ws.rs.core.FeatureContext
;
@Provider
public
public class
class
MaxAgeFeature
MaxAgeFeature
implements
implements
DynamicFeature
{
public
public
void
void
configure
(
ResourceInfo ri
,
FeatureContext ctx
) {
MaxAge max
=
ri
.
getResourceMethod
().
getAnnotation
(
MaxAge
.
class
);
iif
(
max
==
null
return
;
CacheControlFilter filter
=
new
null
)
return
new
CacheControlFilter
(
max
.
value
());
ctx
.
register
(
filter
);
}
}
The
MaxAgeFeature.configure()
method is invoked for every deployed JAX-RS resource
method. The
configure()
method first looks for the
@MaxAge
annotation on the
Re-
sourceInfo
's method. If it exists, it constructs an instance of the
CacheControlFilter
,
passing in the value of the
@MaxAge
annotation. It then registers this created filter with the
FeatureContext
parameter. This filter is now bound to the JAX-RS resource method repres-
ented by the
ResourceInfo
parameter. We've just created a JAX-RS extension!
Name Bindings
The other way to bind a filter or interceptor to a particular JAX-RS method is to use the
@NameBinding
meta-annotation:
package
package
javax
.
ws
.
rs
;
import
import
java.lang.annotation.Documented
java.lang.annotation.Documented
;
import
import
java.lang.annotation.ElementType
java.lang.annotation.ElementType
;
import
import
java.lang.annotation.Retention
java.lang.annotation.Retention
;
import
import
java.lang.annotation.RetentionPolicy
java.lang.annotation.RetentionPolicy
;
import
import
java.lang.annotation.Target
java.lang.annotation.Target
;
@Target
(
ElementType
.
ANNOTATION_TYPE
)
@Retention
(
RetentionPolicy
.
RUNTIME
)
@Documented
public
public
@interface
NameBinding
{
}
You can bind a filter or interceptor to a particular annotation and when that custom annota-
tion is applied, the filter or interceptor will automatically be bound to the annotated JAX-RS