Sunday, November 22, 2009

Creating "alias" URLs for Jersey JAX-RS JSR-311 resources

In REST we are supporting two syntaxes for accessing resources. Let's call it a "fully qualified" resource, and a "shortcut" to the resource. For example, to view a table with its fully qualified URL, you can enter a URL in the format:{account}/db/{db}/tbl/{tbl}

for example:

The "shortcut" URL would be:{account}/{db}/{tbl}

for example

There's a simple "trick" you can apply using JSR-311 to make your resource appear at two different URLs. First, start with your "fully qualified resource". For example, here is snippet:

public class TableResource extends AbstractSelectableResource{
@PathParam("account") protected String account;
@PathParam("db") protected String db;
@PathParam("tbl") protected String table;

Now, to make an "alias" or "shortcut" URL, just define a dummy class that extends the resource you want to make a shortcut to. The purpose of this class is simply to "mount" the resource on an additional URL. Be sure to include all the path parameters and their names must be identical.

public class ShortcutToTableResource extends TableResource{
//this class is intentionally empty

Now your resource will be available on both URLs!

No comments:

Post a Comment