Java Reference
In-Depth Information
@GET
@Path
(
"/uriinfo/{make}/{model}/{year}"
)
@Produces
(
"text/plain"
)
public
public
String
getFromUriInfo
(
@Context
UriInfo info
)
{
String make
=
info
.
getPathParameters
().
getFirst
(
"make"
);
String year
=
info
.
getPathParameters
().
getFirst
(
"year"
);
PathSegment model
=
info
.
getPathSegments
().
get
(
3
);
String color
=
model
.
getMatrixParameters
().
getFirst
(
"color"
);
return
return
"A "
+
color
+
" "
+
year
+
" "
+
make
+
" "
+
model
.
getPath
();
}
The final method,
getFromUriInfo()
, shows how you can obtain the same information us-
ing the
UriInfo
interface. As you can see, the matrix parameter information is extracted
from
PathSegment
instances.
The next piece of code you should look at on the server is
CustomerResource
. This class
shows how
@QueryParam
and
@DefaultValue
can work together to obtain information about
the request URI's query parameters. An example using
UriInfo
is also shown so that you
can see how this can be done without injection annotations:
src/main/java/com/restfully/shop/services/CustomerResource.java
@Path
(
"/customers"
)
public
public class
class
CustomerResource
CustomerResource
{
...
@GET
@Produces
(
"application/xml"
)
public
public
StreamingOutput
getCustomers
(
final
final
@QueryParam
(
"start"
)
int
int
start
,
final
final
@QueryParam
(
"size"
)
@DefaultValue
(
"2"
)
int
int
size
)
{
...
}
The
getCustomers()
method returns a set of customers from the customer database. The
start
parameter defines the start index and the
size
parameter specifies how many custom-
ers you want returned. The
@DefaultValue
annotation is used for the case in which a client
does not use the query parameters to index into the customer list.
The next implementation of
getCustomers()
uses
UriInfo
instead of injection parameters: