Java Reference
In-Depth Information
server will send back a 200, “OK,” response with the new representation of the resource. If it
hasn't been changed, the server will respond with 304, “Not Modified,” and return no repres-
entation. In both cases, the server should send an updated
Cache-Control
and
ETag
header
if appropriate.
One final thing about
ETag
s is they come in two flavors: strong and weak. A strong
ETag
should change whenever any bit of the resource's representation changes. A weak
ETag
changes only on semantically significant events. Weak
ETag
s are identified with a
W/
prefix.
For example:
HTTP
HTTP
/
1.1 200
OOK
Content-Type
:
application/xml
Cache-Control
:
max-age=1000
ETag: W/"3141271342554322343200"
<customer id="123">...</customer>
Weak
ETag
s give applications a bit more flexibility to reduce network traffic, as a cache can
be revalidated when there have been only minor changes to the resource.
JAX-RS has a simple class called
javax.ws.rs.core.EntityTag
that represents the
ETag
header:
public
public class
class
EntityTag
EntityTag
{
public
public
EntityTag
(
String value
) {...}
public
public
EntityTag
(
String value
,
boolean
boolean
weak
) {...}
public
public static
static
EntityTag
valueOf
(
String value
)
throws
throws
IllegalArgumentException
{...}
public
public
boolean
boolean
isWeak
() {...}
public
public
String
getValue
() {...}
}
It is constructed with a string value and optionally with a flag telling the object if it is a weak
ETag
or not. The
getValue()
and
isWeak()
methods return these values on demand.
JAX-RS and conditional GETs
To help with conditional GETs, JAX-RS provides an injectable helper class called
javax.ws.rs.core.Request
:
public
public interface
interface
Request
Request
{
...
ResponseBuilder
evaluatePreconditions
(
EntityTag eTag
);
ResponseBuilder
evaluatePreconditions
(
Date lastModified
);