Java Reference
In-Depth Information
+ address.getZip() + ","
+ address.getCountry() + ","
+ customer.getEmail() + ","
+ customer.getPhone());
properties.store(new FileOutputStream(DATA_STORE),null);
return customerId;
}
...
}
The response returned to the client has a URI to the newly created resource. The return
type is an entity body mapped from the property of the response with the status code spe-
cified by the status property of the response. The
WebApplicationException
is a
RuntimeException
that is used to wrap the appropriate HTTP error status code, such
as 404, 406, 415, or 500.
The
@Consumes({
“
application/xml
”
,
“
application/json
”
})
and
@Pro-
duces({"application/xml","application/json"})
annotations set the
request and response media types to use the appropriate MIME client. These annotations
can be applied to a resource method, a resource class, or even an entity provider. If you do
not use these annotations, JAX-RS allows the use of any media type (“
*/*
”).
The following code snippet shows the implementation of the
getCustomer
and
find-
byId
methods. The
getCustomer
method uses the
@Produces
annotation and re-
turns a
Customer
object, which is converted to an XML or JSON representation depend-
ing on the
Accept:
header specified by the client.
@GET
@Path("{id}")
@Produces({"application/xml", "application/json"})
public Customer getCustomer(@PathParam("id") String customerId) {
Customer customer = null;
try {
customer = findById(customerId);
} catch (Exception ex) {
logger.log(Level.SEVERE,
"Error calling searchCustomer() for customerId {0}.
{1}",
new Object[]{customerId, ex.getMessage()});