Java Reference
In-Depth Information
Restricting
the
Structure
of
Values
in
a
Path
Template
Problem
You don't want just a simple template that accepts any string (such as /products/{id})
to match your resource; you need to define a template with a more powerful mechanism
for handling variation, and ensure that only values matching a certain complex structure are
passed to your method.
Solution
Use a regular expression after a colon in your template, like this: @Path("products/{id}:
[a-zA-Z][a-zA-Z_0-9]}"} . Parameters that don't match the expression will return a 404
HTTP status code.
Discussion
JAX-RS path parameters allow you to define your templates using regular expressions. Just
create your path template as you normally would, but add a colon and then your regular ex-
pression within the path.
NOTE
You need to use the latest version of the Jersey implementation for this functionality. Version 0.8,
which may be included by default in the plug-in for your IDE (as it was the first general release),
does not support this. Version 1.0 supports regular expressions.
Example 8-9 provides an illustration.
Example8-9.A path template using a regular expression
@Path("/products/{id: \\d{3}}")
public class ProductResource {
public ProductResource() { }
@GET
@Produces("text/plain")
public String getProductPlainText(@PathParam("id") int productId) {
Search WWH ::




Custom Search