Java Reference
In-Depth Information
cust
.
setStreet
(
update
.
getStreet
());
cust
.
setState
(
update
.
getState
());
cust
.
setZip
(
update
.
getZip
());
cust
.
setCountry
(
update
.
getCountry
());
builder
=
Response
.
noContent
();
return
return
builder
.
build
();
}
}
Finally, the update is performed.
The Client Code
The client code first performs a conditional GET. It then tries to do a conditional PUT using
a bad
ETag
value.
public
public class
class
CustomerResourceTest
CustomerResourceTest
{
@Test
public
public
void
void
testCustomerResource
()
throws
throws
Exception
{
WebTarget customerTarget
=
client
.
target
(
"http://localhost:8080/services/customers/1"
);
Response response
=
customerTarget
.
request
().
get
();
Assert
.
assertEquals
(
200
,
response
.
getStatus
());
Customer cust
=
response
.
readEntity
(
Customer
.
class
);
EntityTag etag
=
response
.
getEntityTag
();
response
.
close
();
The
testCustomerResource()
method starts off by fetching a preinitialized
Customer
ob-
ject. It does this so that it can obtain the current
ETag
of the
Customer
representation.
System
.
out
.
println
(
"Doing a conditional GET with ETag: "
+
etag
.
toString
());
response
=
customerTarget
.
request
()
.
header
(
"If-None-Match"
,
etag
).
get
();
Assert
.
assertEquals
(
304
,
response
.
getStatus
());
response
.
close
();
This code is performing a conditional GET. We set the
If-None-Match
header using the pre-
viously fetched
ETag
value. The client is expecting that the server will return a 304, “Not
Modified,” response.