Java Reference
In-Depth Information
Scope of Path Parameters
Sometimes a named URI path parameter will be repeated by different
@Path
expressions that
compose the full URI matching pattern of a resource method. The path parameter could be
repeated by the class's
@Path
expression or by a subresource locator. In these cases, the
@PathParam
annotation will always reference the final path parameter. For example:
@Path
(
"/customers/{id}"
)
public
public class
class
CustomerResource
CustomerResource
{
@Path
(
"/address/{id}"
)
@Produces
(
"text/plain"
)
@GET
public
public
String
getAddress
(
@PathParam
(
"id"
)
String addressId
) {...}
}
If our HTTP request was
GET /customers/123/address/456
, the
addressId
parameter in
the
getAddress()
method would have the
456
value injected.
PathSegment and Matrix Parameters
@PathParam
can not only inject the value of a path parameter, it can also inject instances of
javax.ws.rs.core.PathSegment
. The
PathSegment
class is an abstraction of a specific
URI path segment:
package
package
javax
.
ws
.
rs
.
core
;
public
public interface
interface
PathSegment
PathSegment
{
String
getPath
();
MultivaluedMap
<
String
,
String
>
getMatrixParameters
();
}
The
getPath()
method is the string value of the actual URI segment minus any matrix para-
meters. The more interesting method here is
getMatrixParameters()
. This returns a map
of all of the matrix parameters applied to a particular URI segment. In combination with
@PathParam
, you can get access to the matrix parameters applied to your request's URI. For
example:
@Path
(
"/cars/{make}"
)
public
public class
class
CarResource
CarResource
{
@GET