Java Reference
In-Depth Information
@Path
(
"/{model}/{year}"
)
@Produces
(
"image/jpeg"
)
public
public
Jpeg
getPicture
(
@PathParam
(
"make"
)
String make
,
@PathParam
(
"model"
)
PathSegment car
,
@PathParam
(
"year"
)
String year
) {
String carColor
=
car
.
getMatrixParameters
().
getFirst
(
"color"
);
...
}
In this example, we have a
CarResource
that allows us to get pictures of cars in our data-
base. The
getPicture()
method returns a JPEG image of cars that match the make, model,
and year that we specify. The color of the vehicle is expressed as a matrix parameter of the
model. For example:
GET
/
cars
/
mercedes
/
e55
;
color
=
black
/
2006
Here, our make is
mercedes
, the model is
e55
with a color attribute of
black
, and the year is
2006
. While the make, model, and year information can be injected into our
getPicture()
method directly, we need to do some processing to obtain information about the color of the
vehicle.
Instead of injecting the model information as a Java string, we inject the path parameter as a
PathSegment
into the
car
parameter. We then use this
PathSegment
instance to obtain the
color
matrix parameter's value.
Matching with multiple PathSegments
Sometimes a particular path parameter matches to more than one URI segment. In these
cases, you can inject a list of
PathSegments
. For example, let's say a model in our
CarRe-
source
could be represented by more than one URI segment. Here's how the
getPicture()
method might change:
@Path
(
"/cars/{make}"
)
public
public class
class
CarResource
CarResource
{
@GET
@Path
(
"
/
{
model
: .+}
/
year
/{
year
}
")
@Produces("
image
/
jpeg
")
public Jpeg getPicture(@PathParam("
make
"
)
String make
,
@PathParam
(
"model"
)
List
<
PathSegment
>
car
,
@PathParam
(
"year"
)
String year
) {
}
}