Java Reference
In-Depth Information
Chapter 25. Examples for
Chapter 11
In
Chapter 11
,
you learned about HTTP caching techniques. Servers can tell HTTP clients if
and how long they can cache retrieved resources. You can revalidate expired caches to avoid
resending big messages by issuing conditional GET invocations. Conditional PUT operations
can be invoked for safe concurrent updates.
Example ex11_1: Caching and Concurrent Updates
The example in this chapter expands on the
CustomerResource
example repeated
throughout this topic to support caching, conditional GETs, and conditional PUTs.
The Server Code
The first thing is to add a
hashCode()
method to the
Customer
class:
src/main/java/com/restfully/shop/domain/Customer.java
@XmlRootElement
(
name
=
"customer"
)
public
public class
class
Customer
Customer
{
...
@Override
public
public
int
int
hashCode
()
{
int
int
result
=
id
;
result
=
31
*
result
+ (
firstName
!=
null
null
?
firstName
.
hashCode
() :
0
);
result
=
31
*
result
+ (
lastName
!=
null
null
?
lastName
.
hashCode
() :
0
);
result
=
31
*
result
+ (
street
!=
null
null
?
street
.
hashCode
() :
0
);
result
=
31
*
result
+ (
city
!=
null
null
?
city
.
hashCode
() :
0
);
result
=
31
*
result
+ (
state
!=
null
null
?
state
.
hashCode
() :
0
);
result
=
31
*
result
+ (
zip
!=
null
null
?
zip
.
hashCode
() :
0
);
result
=
31
*
result
+ (
country
!=
null
null
?
country
.
hashCode
() :
0
);
return
return
result
;
}
}
This method is used in the
CustomerResource
class to generate semi-unique
ETag
header
values. While a hash code calculated in this manner isn't guaranteed to be unique, there is a