users@jersey.java.net

Re: One Resource class for two URLs

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Tue, 22 Apr 2008 11:01:15 +0200

On Apr 22, 2008, at 8:16 AM, Martin Probst wrote:

> Hi,
>
>> @Path("/")
>> public class SomeResource {
>> [...]
>
> Thanks, this might work. Though I'll have to try and see how this
> works with implicit views and the implicit trailing part of the
> request, i.e. if /books/ maps to a class Books, /books/new will
> render the new.jsp (for example).
>

Implicit views are resolved from the resource class name. You need a
class associated with a resource for implicit views to work from the
URI associated with that resource.

An implicit view to "/books/new" is equivalent to the explicit approach:

   public class Books {
     @GET
     @Path("new")
     public Viewable getNew() {
       return new Viewable("new", this);
     }
  }

We might be able to improve this so that the Viewable does not have
to make explicit reference to the view, and if not it is picked up
from the last path segment. Perhaps the same for the model, for example:

   return new Viewable()

Could mean get the template name from the path and the model is the
resource that returned the Viewable.


>> although I would not recommend that approach. Think a bit about how
>> your resource are going to be used - i.e:
>>
>> 1) Get all books: /books/
>> 2) From the list of books show detailed info about a particular
>> one: /books/42
>
> Is there a way do discriminate between the /books/42 usage and a /
> books/new URL? Is there some kind of order in processing or a way
> to further specify the URL patterns, e.g. by regexp?
>

Explicit matching on @Path always takes precedence over implicit view
matching. You either need to be explicit (as shown above) or have the
following URLs:

   /books
   /books/new
   /books/book/{id}


> Another way might be returning this on some matching part, e.g.:
>
> @Path("/")
> class Books {
> @Path("books") getBooks() { return this; }
> @Path("book/{id}") getBook(long id) { ... }
> }
>

If you could have "/books" and "/books/book/{id}" it may make things
easier for you.

@Path("books")
class Books {
    @Path("book/{id}") getBook(...) { ... }
}

It adds a bit of redundancy to the URI space but i still think it is
"cool" enough (in terms of Cool URIs).

Paul.