Java Reference
In-Depth Information
Chapter 22. Examples for
Chapter 7
seBuilder
classes. You also learned how to map thrown exceptions to a
Response
using a
javax.ws.rs.ext.ExceptionMapper
. Since most of our examples use a
Response
object in
one way or another, this chapter focuses only on writing an
ExceptionMapper
.
Example ex07_1: ExceptionMapper
This example is a slight modification from
ex06_1
to show you how you can use
Excep-
tionMappers
. Let's take a look at the
CustomerResource
class to see what is different:
src/main/java/com/restfully/shop/services/CustomerResource.java
@Path
(
"/customers"
)
public
public class
class
CustomerResource
CustomerResource
{
...
@GET
@Path
(
"{id}"
)
@Produces
(
"application/xml"
)
public
public
Customer
getCustomer
(
@PathParam
(
"id"
)
int
int
id
)
{
Customer customer
=
customerDB
.
get
(
id
);
iif
(
customer
==
null
null
)
{
throw new
CustomerNotFoundException
(
"Could not find customer "
+
id
);
}
return
return
customer
;
}
@PUT
@Path
(
"{id}"
)
@Consumes
(
"application/xml"
)
public
public
void
void
updateCustomer
(
@PathParam
(
"id"
)
int
int
id
,
Customer update
)
{
Customer current
=
customerDB
.
get
(
id
);
if
(
current
==
null
)
throw new
CustomerNotFoundException
(
"Could not find customer "
+
id
);
current
.
setFirstName
(
update
.
getFirstName
());