URI Templates in RESTEasy

One nice feature of RESTEasy is its support for URI Templates. The URI Template spec defines a means of embedding variables in the URI. So the following URI Template:

http://somehost/contacts/contactId

Could return an XML document for a contact where the contactId=234:

http://somehost/contacts/234

In this example, we might be executing the following Java method:

@HttpMethod("GET")
public Contact getContactById(@URLParam("contactId") Long contactId) {
    Contact contact = entityManager.find(Contact.class, contactId);
    return contact;
}

Since the contactId parameter is a Long, RESTEasy will assign a regular expression that matches a digit rather than a String value. The effect of this is that if you try and access:

http://somehost/contacts/some_person

A 404 error is returned rather than a 500. In addition to numeric URI template types, RESTEasy will support custom types as well.

Advertisement