Java Reference
In-Depth Information
+
make
+
" "
+
car
.
getPath
();
}
The
getFromMatrixParam()
method uses the
@MatrixParam
annotation to inject the matrix
parameter
color
. An example of a URI it could process is
/cars/matrix/mercedes/
e55;color=black/2006
. Notice that it automatically converts the matrix parameter into the
Java enum
Color
:
@GET
@Path
(
"/segment/{make}/{model}/{year}"
)
@Produces
(
"text/plain"
)
public
public
String
getFromPathSegment
(
@PathParam
(
"make"
)
String make
,
@PathParam
(
"model"
)
PathSegment car
,
@PathParam
(
"year"
)
String year
)
{
String carColor
=
car
.
getMatrixParameters
().
getFirst
(
"color"
);
return
return
"A "
+
carColor
+
" "
+
year
+
" "
+
make
+
" "
+
car
.
getPath
();
}
The
getFromPathSegment()
method also illustrates how to extract matrix parameter in-
formation. Instead of using
@MatrixParam
, it uses an injected
PathSegment
instance repres-
enting the
model
path parameter to obtain the matrix parameter information:
@GET
@Path
(
"/segments/{make}/{model : .+}/year/{year}"
)
@Produces
(
"text/plain"
)
public
public
String
getFromMultipleSegments
(
@PathParam
(
"make"
)
String make
,
@PathParam
(
"model"
)
List
<
PathSegment
>
car
,
@PathParam
(
"year"
)
String year
)
{
String output
=
"A "
+
year
+
" "
+
make
;
for
for
(
PathSegment segment
:
car
)
{
output
+=
" "
+
segment
.
getPath
();
}
return
return
output
;
}
The
getFromMultipleSegments()
method illustrates how a path parameter can match mul-
tiple segments of a URI. An example of a URI that it could process is
/cars/segments/
mercedes/e55/amg/year/2006
. In this case,
e55/amg
would match the
model
path para-
meter. The example injects the
model
parameter into a list of
PathSegment
instances: