Java Reference
In-Depth Information
public
public
void
void
setNoStore
(
boolean
boolean
noStore
) {...}
public
public
Map
<
String
,
String
>
getCacheExtension
() {...}
}
The
ResponseBuilder
class has a method called
cacheControl()
that can accept a
CacheControl
object:
@Path
(
"/customers"
)
public
public class
class
CustomerResource
CustomerResource
{
@Path
(
"{id}"
)
@GET
@Produces
(
"application/xml"
)
public
public
Response
getCustomer
(
@PathParam
(
"id"
)
int
int
id
) {
Customer cust
=
findCustomer
(
id
);
CacheControl cc
=
new
CacheControl
();
cc
.
setMaxAge
(
300
);
cc
.
setPrivate
(
true
);
cc
.
setNoStore
(
true
);
ResponseBuilder builder
=
Response
.
ok
(
cust
,
"application/xml"
);
builder
.
cacheControl
(
cc
);
return
return
builder
.
build
();
}
In this example, we initialize a
CacheControl
object and pass it to the
ResponseBuild-
er.cacheControl()
method to set the
Cache-Control
header of the response. Unfortu-
nately, JAX-RS doesn't yet have any nice annotations to do this for you automatically.
Revalidation and Conditional GETs
One interesting aspect of the caching protocol is that when the cache is stale, the cacher can
ask the server if the data it is holding is still valid. This is called
revalidation
. To be able to
perform revalidation, the client needs some extra information from the server about the re-
source it is caching. The server will send back a
Last-Modified
and/or an
ETag
header with
its initial response to the client.
Last-Modified
The
Last-Modified
header represents a timestamp of the data sent by the server. Here's an
example response:
HTTP
/
1.1 200
OK
Content
-
Type:
application
/
xml
Cache
-
Control:
max
-
age
=
1000