Java Reference
In-Depth Information
@GET
@Produces
(
"application/xml"
)
@Path
(
"uriinfo"
)
public
public
StreamingOutput
getCustomers
(
@Context
UriInfo info
)
{
int
int
start
=
0
;
int
int
size
=
2
;
iif
(
info
.
getQueryParameters
().
containsKey
(
"start"
))
{
start
=
Integer
.
valueOf
(
info
.
getQueryParameters
().
getFirst
(
"start"
));
}
iif
(
info
.
getQueryParameters
().
containsKey
(
"size"
))
{
size
=
Integer
.
valueOf
(
info
.
getQueryParameters
().
getFirst
(
"size"
));
}
return
return
getCustomers
(
start
,
size
);
}
As you can see, the code to access query parameter data programmatically is a bit more verb-
ose than using injection annotations.
The Client Code
The client code for this example lives in the file
src/test/java/com/restfully/shop/test/Injec-
tionTest.java
. The code is quite boring, so I won't get into too much detail.
The
testCarResource()
method invokes these requests on the server to test the
CarRe-
source
class:
GET http://localhost:8080/services/cars/matrix/mercedes/e55;color=black/2006
GET http://localhost:8080/services/cars/segment/mercedes/e55;color=black/2006
GET http://localhost:8080/services/cars/segments/mercedes/e55/amg/year/2006
GET http://localhost:8080/services/cars/uriinfo/mercedes/e55;color=black/2006
The
testCustomerResource()
method invokes these requests on the server to test the
Cus-
tomerResource
class:
GET http://localhost:8080/services/customers
GET http://localhost:8080/services/customers?start=1&size=3
GET http://localhost:8080/services/customers/uriinfo?start=2&size=2
The request without query parameters shows
@DefaultValue
in action. It is worth noting
how query parameters are handled in the client code. Let's look at
testCustomerRe-
source()
a little bit: