Java Reference
In-Depth Information
This class accepts HTTP
HEAD /shop
requests and publishes the
customers
and
orders
link
relationships. These links point to the services represented by the
CustomerResource
and
OrderResource
classes.
The Client Code
The client code creates a new customer and order. It then cancels the order, purges it, and, fi-
nally, relists the order entry database. All URLs are accessed via
Link
headers or Atom links:
public
public class
class
OrderResourceTest
OrderResourceTest
{
@Test
public
public
void
void
testCreateCancelPurge
()
throws
throws
Exception
{
String base
=
"http://localhost:8080/services/shop"
;
Response response
=
client
.
target
(
base
).
request
().
head
();
Link customers
=
response
.
getLink
(
"customers"
);
Link orders
=
response
.
getLink
(
"orders"
);
response
.
close
();
The
testCreateCancelPurge()
method starts off by doing a HEAD request to
/shop
to ob-
tain the service links provided by our application. The
Response.getLink()
method allows
you to query for a
Link
header sent back with the HTTP response.
System
.
out
.
println
(
"** Create a customer through this URL: "
+
customers
.
getHref
());
Customer customer
=
new
new
Customer
();
customer
.
setFirstName
(
"Bill"
);
customer
.
setLastName
(
"Burke"
);
customer
.
setStreet
(
"10 Somewhere Street"
);
customer
.
setCity
(
"Westford"
);
customer
.
setState
(
"MA"
);
customer
.
setZip
(
"01711"
);
customer
.
setCountry
(
"USA"
);
response
=
client
.
target
(
customers
).
request
().
post
(
Entity
.
xml
(
customer
));
Assert
.
assertEquals
(
201
,
response
.
getStatus
());
response
.
close
();
We create a customer in the customer database by POSTing an XML representation to the
URL referenced in the
customers
link relationship. This relationship is retrieved from our
initial HEAD request to
/shop
.