For this section, I’ll illustrate the use of an @Path annotation with regular expressions. The example is a direct copy of the code in ex03_1 with a few minor modifications.
Perform the following steps:
The CustomerResource class copied from the ex03_1 example is pretty much the same in ex04_2, except that a few of the @Path expressions have been modified. I also added an extra method that allows you to reference customers by their first and last names within the URL path:
@Path("/customers")
public class CustomerResource {
...
@GET
@Path("{id : \\d+}")
@Produces("application/xml")
public StreamingOutput getCustomer(@PathParam("id") int id)
{
...
}
@PUT
@Path("{id : \\d+}")
@Consumes("application/xml")
public void updateCustomer(@PathParam("id") int id, InputStream is)
{
...
}
The @Path expression for getCustomer() and updateCustomer() was changed a little bit to use a Java regular expression for the URI matching. The expression dictates that the id segment of the URI can only be a string of digits. So, /customers/333 is a legal URI, but /customers/a32ab would result in a 404, “Not Found,” response code being returned to the client:
@GET
@Path("{first : [a-zA-Z]+}-{last:[a-zA-Z]+}")
@Produces("application/xml")
public StreamingOutput getCustomerFirstLast(
@PathParam("first") String first,
@PathParam("last") String last)
{
...
}
To show a more complex regular expression, I added the getCustomerFirstLast() method to the resource class. This method provides a URI pointing to a specific customer, using the customer’s first and last names instead of a numeric ID. This @Path expression matches a string of the first name and last name separated by a hyphen character. A legal URI is /customers/Bill-Burke. The name can only have letters within it, so /customers/Bill7-Burke would result in a 404, “Not Found,” being returned to the client.
The client code is in src/test/java/com/restfully/shop/test/ClientResourceTest.java. It is really not much different than the code in example ex03_1, other than the fact that it additionally invokes the URI represented by the getCustomerFirstLast() method. If you’ve examined the code from Chapter 18, you can probably understand what is going on in this client example, so I won’t elaborate further.