Java Reference
In-Depth Information
@PUT
@Path
(
"{id}"
)
@Consumes
(
"application/xml"
)
public
public
void
int
id
,
InputStream is
) {
Customer update
=
readCustomer
(
is
);
Customer current
=
customerDB
.
get
(
id
);
iif
(
current
==
null
void
updateCustomer
(
@PathParam
(
"id"
)
int
null
)
throw
throw new
new
WebApplicationException
(
Response
.
Status
.
NOT_FOUND
);
current
.
setFirstName
(
update
.
getFirstName
());
current
.
setLastName
(
update
.
getLastName
());
current
.
setStreet
(
update
.
getStreet
());
current
.
setState
(
update
.
getState
());
current
.
setZip
(
update
.
getZip
());
current
.
setCountry
(
update
.
getCountry
());
}
We annotate the
updateCustomer()
method with
@javax.ws.rs.PUT
to bind HTTP PUT
requests to this method. Like our
getCustomer()
method,
updateCustomer()
is annotated
with an additional
@Path
annotation so that we can match
/customers/{id}
URIs.
The
updateCustomer()
method takes two parameters. The first is an
id
parameter that rep-
resents the
Customer
object we are updating. Like
getCustomer()
, we use the
@PathParam
annotation to extract the ID from the incoming request URI. The second parameter is an
In-
putStream
that will allow us to read in the XML document that was sent with the PUT re-
quest. Like
createCustomer()
, a parameter that is not annotated with a JAX-RS annotation
is considered a representation of the body of the incoming message.
In the first part of the method implementation, we read in the XML document and create a
Customer
object out of it. The method then tries to find an existing
Customer
object in the
customerDB
map. If it doesn't exist, we throw a
WebApplicationException
that will send a
404, “Not Found,” response code back to the client. If the
Customer
object does exist, we
update our existing
Customer
object with new updated values.
Utility methods
The final thing we have to implement is the utility methods that were used in
createCus-
tomer()
,
getCustomer()
, and
updateCustomer()
to transform
Customer
objects to and
from XML. The
outputCustomer()
method takes a
Customer
object and writes it as XML
to the response's
OutputStream
: