Java Reference
In-Depth Information
}
}
In this example, we inject an instance of UriInfo into the getPicture() method's info
parameter. We then use this instance to extract information out of the URI.
@MatrixParam
Instead of injecting and processing PathSegment objects to obtain matrix parameter values,
the JAX-RS specification allows you to inject matrix parameter values directly through the
@javax.ws.rs.MatrixParam annotation. Let's change our CarResource example from the
previous section to reflect using this annotation:
@Path ( "/{make}" )
public
public class
class CarResource
CarResource {
@GET
@Path ( "/{model}/{year}" )
@Produces ( "image/jpeg" )
public
public Jpeg getPicture ( @PathParam ( "make" ) String make ,
@PathParam ( "model" ) String model ,
@MatrixParam ( "color" ) String color ) {
...
}
Using the @MatrixParam annotation shrinks our code and provides a bit more readability.
The only downside of @MatrixParam is that sometimes you might have a repeating matrix
parameter that is applied to many different path segments in the URI. For example, what if
color shows up multiple times in our car service example?
GET / mercedes / e55 ; color = black / 2006 / interior ; color = tan
Here, the color attribute shows up twice: once with the model and once with the interior.
Using @MatrixParam("color") in this case would be ambiguous and we would have to go
back to processing PathSegments to obtain this matrix parameter.
@QueryParam
The @javax.ws.rs.QueryParam annotation allows you to inject individual URI query para-
meters into your Java parameters. For example, let's say we wanted to query a customer
database and retrieve a subset of all customers in the database. Our URI might look like this:
GET / customers ? start = 0 & size = 10
Search WWH ::




Custom Search