@PathParam Annotation with Regular Expressions
JAX-RS provides a rich set of annotations to extract the different parameters that a request could send (@PathParam, @QueryParam, @MatrixParam, @CookieParam, @HeaderParam, and @FormParam).
@PathParam annotation is used to extract the value of a URI template parameter.
A parameter has a name and is represented by a variable between curly braces or by a variable that follows a regular expression.
@Path("/item")
@Produces(MediaType.APPLICATION_JSON)
public class ItemRestService {
@Path("search/{text}")
public Items searchItems(@PathParam("text") String textToSearch) {
// URI : /item/search/smith
}
@GET
@Path("{login: [a-zA-Z]*}")
public Item getItemByLogin(@PathParam("login") String login) {
// URI : /item/mycake
}
@GET
@Path("{itemId : \\d+}")
public Customer getItemById(@PathParam("itemId") Long id) {
// URI : /item/1122
}
}
The searchItems method takes any String parameter while getItemByLogin only allows
lowercase/uppercase alphabetical letters ([a-zA-Z]*) and getItemById only digits (\\d+).
Ref: Beginning Java EE7