users@jersey.java.net

[Jersey] URI to Resource mapping

From: <franck102_at_yahoo.com>
Date: Fri, 17 Aug 2012 14:37:15 +0000 (GMT)

Hi,

I am new to Jersey, and struggling to come up with the correct design
patterns for my resource hierarchy, especially w.r.t subresources,
injection and URIs.

I have a fairly simple multi-level hierarchy, let's say bookstore -
sections - books and unique IDs for sections & books:
/bookstore/sections/adventure
/bookstore/sections/adventure/books/some-book-id

I have three specific questions:

1. I have a sections resource locator in the bookstore resource with an
"sectionID" path param. I have found various versions on the web of the
proper code to instantiate the subresource so that it can benefit from
injection; including a post mentioning having to use a Scope of
"prototype" if more then one subresource is to be instantiated in the
request. Most of the posts are pretty old, so I am unclear as to
whether they are still valid? Is this pattern the recommended approach:

@Path("sections/{sectionId}")
public SectionResource getSection(@PathParam("sectionId") String
sectionId)
{
                SectionResource res =
                    
_resourceContext.getResource(SectionResource.class);
                res.setId(sectionId);
                return res;
}
Is there any way to have the sectionId injected (to avoid the setId
call)?
I can't find any documentation about a prototype scope, what is the
story there?

2. When processing a PUT of a property on a book (e.g. genre) I need to
access the book's section model. Would
ResourceContext.matchResource(".../bookstore/sections/adventure")
return a SectionResource properly initialized for "adventure"? i.e.
will it call my locator and set the sectionId?
Knowing that the book is a subresource of its section, is there a
better way to access the parent resource (other than an explicit
setSection in the book's locator)?

3. Each book holds a URI to an author resource that lives under
/bookstore/authors/some-author.
When receiving a PUT request to update the author of a book, can I use
ResourceContext.matchResource(<supplied author URI>) to validate that
the author exists?

Any help will be greatly appreciated!
Thanks