Java Reference
In-Depth Information
Order order
=
new
new
Order
();
order
.
setTotal
(
"$199.99"
);
order
.
setCustomer
(
customer
);
order
.
setDate
(
new
new
Date
().
toString
());
LineItem item
=
new
new
LineItem
();
item
.
setCost
(
"$199.99"
);
item
.
setProduct
(
"iPhone"
);
order
.
setLineItems
(
new
new
ArrayList
<
LineItem
>());
order
.
getLineItems
().
add
(
item
);
System
.
out
.
println
();
System
.
out
.
println
(
"** Create an order through this URL: "
+
orders
.
getUri
().
toString
());
response
=
client
.
target
(
orders
).
request
().
post
(
Entity
.
xml
(
order
));
Assert
.
assertEquals
(
201
,
response
.
getStatus
());
URI createdOrderUrl
=
response
.
getLocation
();
response
.
close
();
Next, we create an order entry by posting to the
orders
link relationship. The URL of the
created order is extracted from the returned
Location
header. We will need this later when
we want to cancel this order:
System
.
out
.
println
();
System
.
out
.
println
(
"** New list of orders"
);
response
=
client
.
target
(
orders
).
request
().
get
();
String orderList
=
response
.
readEntity
(
String
.
class
);
System
.
out
.
println
(
orderList
);
Link purge
=
response
.
getLink
(
"purge"
);
response
.
close
();
A
GET /orders
request is initiated to show all the orders posted to the system. We extract the
purge
link returned by this invocation so it can be used later when the client wants to purge
cancelled orders:
response
=
client
.
target
(
createdOrderUrl
).
request
().
head
();
Link cancel
=
response
.
getLink
(
"cancel"
);
response
.
close
();
Next, the client cancels the order that was created earlier. A HEAD request is made to the
created order's URL to obtain the
cancel
link relationship:
iif
(
cancel
!=
null
null
)
{
System
.
out
.
println
(
"** Cancelling the order at URL: "
+
cancel
.
getUri
().
toString
());
response
=
client
.
target
(
cancel
).
request
().
post
(
null
null
);