Java Reference
In-Depth Information
method that allows you to reference customers by their first and last names within the URL
path:
@Path
(
"/customers"
)
public
public class
class
CustomerResource
CustomerResource
{
...
@GET
@Path
(
"{id : \\d+}"
)
@Produces
(
"application/xml"
)
public
public
StreamingOutput
getCustomer
(
@PathParam
(
"id"
)
int
int
id
)
{
...
}
@PUT
@Path
(
"{id : \\d+}"
)
@Consumes
(
"application/xml"
)
public
public
void
void
updateCustomer
(
@PathParam
(
"id"
)
int
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
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
/cus-