Java Reference
In-Depth Information
Let's say that in our example, we have two customer databases with different kinds of identi-
fiers. One database uses a numeric key, as we talked about before. The other uses first and
last name as a composite key. We would need to have two different classes to extract the ap-
propriate information from the URI. Let's change our example:
@Path
(
"/customers"
)
public
public class
class
CustomerDatabaseResource
CustomerDatabaseResource
{
protected
protected
CustomerResource europe
=
new
new
CustomerResource
();
protected
protected
FirstLastCustomerResource northamerica
=
new
new
FirstLastCustomerResource
();
@Path
(
"{database}-db"
)
public
public
Object
getDatabase
(
@PathParam
(
"database"
)
String db
) {
iif
(
db
.
equals
(
"europe"
)) {
return
return
europe
;
}
else
else
if
(
db
.
equals
(
"northamerica"
)) {
return
return
northamerica
;
}
else
else return
return null
null
;
}
}
Instead of our
getDatabase()
method returning a
CustomerResource
, it will return any
java.lang.Object
. JAX-RS will introspect the instance returned to figure out how to dis-
patch the request. For this example, if our database is
europe
, we will use our original
Cus-
tomerResource
class to service the remainder of the request. If our database is
northamer-
ica
, we will use a new subresource class
FirstLastCustomerResource
:
public
public class
class
FirstLastCustomerResource
FirstLastCustomerResource
{
private
private
Map
<
String
,
Customer
>
customerDB
=
new
new
ConcurrentHashMap
<
String
,
Customer
>();
@GET
@Path
(
"{first}-{last}"
)
@Produces
(
"application/xml"
)
public
public
StreamingOutput
getCustomer
(
@PathParam
(
"first"
)
String firstName
,
@PathParam
(
"last"
)
String lastName
) {
...
}
@PUT
@Path
(
"{first}-{last}"
)
@Consumes
(
"application/xml"
)