users@jersey.java.net

[Jersey] Re: Decoding URI into path parameters

From: Martin Matula <martin.matula_at_oracle.com>
Date: Wed, 16 Mar 2011 22:51:56 +0100

Hi Gili,

On Mar 16, 2011, at 4:47 PM, Gili wrote:

> If a user invokes HTTP PUT with a body that contains URIs, how should I
> translate those URIs back to resource identifiers? Am I expected to hit the
> URI, examine the body in order to figure out the resource identity or should
> I parse the URI to an ID somehow? What's the best practice?

You can use UriTemplate (see http://jersey.java.net/nonav/apidocs/1.5/jersey/com/sun/jersey/api/uri/UriTemplate.html).

For example, like this:

        // create URI template object for a given template
        UriTemplate ut = new UriTemplate("http://localhost:8080/context/jersey/books/{id}/page/{pageNum}");
        // this hashmap will be populated with param name->value mapping after the URI is matched
        HashMap<String, String> m = new HashMap<String, String>();
        // match the URI passed in the first parameter, populate the map in the second parameter with params and values
        ut.match("http://localhost:8080/context/jersey/books/20/page/10", m);
        // extract book ID from the URI
        int bookId = m.get("id");

Is that good enough for what you need?
Martin