Sunday, November 22, 2009

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

In NextDB.net 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:

http://nextdb.net/nextdb/rest/home/{account}/db/{db}/tbl/{tbl}

for example: http://nextdb.net/nextdb/rest/home/geoff/db/testchars/tbl/lines

The "shortcut" URL would be:
http://nextdb.net/nextdb/rest/{account}/{db}/{tbl}

for example http://nextdb.net/nextdb/rest/geoff/testchars/lines

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:

@Path("/home/{account}/db/{db}/tbl/{tbl}")
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.

@Path("/{account}/{db}/{tbl}")
public class ShortcutToTableResource extends TableResource{
//this class is intentionally empty
}

Now your resource will be available on both URLs!

No comments:

Post a Comment