Java Reference
In-Depth Information
Chapter 26. Examples for
Chapter 12
In
Chapter 12
,
you learned how filters and interceptors can be used to augment your JAX-RS
service classes. In this chapter, we through how to build and run some of the examples
shown in that chapter. Specifically, we'll go write a
ContainerResponseFilter
, a
Dynam-
icFeature
, and an implementation of a
WriterInterceptor
. If you want to see examples
of a
ClientRequestFilter
and a
ContainerRequestFilter
bound via a
@NameBinding
,
check out
Chapter 29
.
Example ex12_1 : ContainerResponseFilter and
DynamicFeature
The Server Code
The
@MaxAge
,
CacheControlFilter
, and
MaxAgeFeature
classes were explained pretty
well in
DynamicFeature
,
so I'm not going to go into them again here. We applied the
@MaxAge
annotation to the
CustomerResource.getCustomer()
method:
src/main/java/com/restfully/shop/services/CustomerResource
@GET
@Path
(
"{id}"
)
@Produces
(
"application/xml"
)
@MaxAge
(
500
)
public
public
Customer
getCustomer
(
@PathParam
(
"id"
)
int
int
id
)
{
Customer customer
=
customerDB
.
get
(
id
);
iif
(
customer
==
null
null
)
{
throw
throw new
new
WebApplicationException
(
Response
.
Status
.
NOT_FOUND
);
}
return
return
customer
;
}
Applying this annotation to this method will cause the
CacheControlFilter
to be bound to
this method when it is executed. The filter will cause a
Cache-Control
header to be added to