users@jersey.java.net

Re: How to retrieve list of resources - use MatrixParams?

From: Marc Hadley <Marc.Hadley_at_Sun.COM>
Date: Tue, 11 Mar 2008 09:50:11 -0400

On Mar 11, 2008, at 4:22 AM, Martin Grotzke wrote:
>
> thanx for your feedback! Would it be easier if I used s.th.
> like /products/123,234,345?
>
Easiest would be something like

/products?p=123&p=234&p=345

then you could use

UriInfo.getQueryParameters.get("p")

which returns a List<String> with values {"123","234","345"}

> Shouldn't such a basic concept of REST be supported a little bit
> better
> - with a higher level of abstraction? If I would have to bother with
> PathSegements it would be more intuitive to use comma, and simply do a
> string.split. But this doesn't seem to be what it should be...
>
If you used commas then you could define your own type:

public class ProductList<String> extends ArrayList<String> {
    public ProductList(String commaSeparatedList) {
       // do the string split and add elements
    }
}

Then you can write

@Path("products/{ids}")
public class ProductListResource {
   @GET
   public XXX get(@PathParam("ids") ProductList<String> list) {...}
}


> So is this an outstanding issue of the spec?
>
I don't think so, the spec already supports the ability to add types
to support a variety of URI styles.

Marc.


>
> On Mon, 2008-03-10 at 17:03 -0400, Marc Hadley wrote:
>> On Mar 10, 2008, at 4:45 PM, Martin Grotzke wrote:
>>>
>>> I want to allow the user to retrieve a list of specified products
>>> under
>>> a URL like this: /products/123;234;345
>>>
>>> How is this possible with jersey?
>>>
>>
>> Semicolon is used to mark the start of matrix parameters in a URI
>> path
>> segment. Try something like:
>>
>> @Path("/products/{id}")
>> public class ProductList {
>>
>> @Context
>> UriInfo uri;
>>
>> @GET
>> public XXX get(@PathParam("id") String id) {
>> // id contains the first product (123 above)
>> List<PathSegment> segs = uri.getPathSegments();
>> PathSegment p = segs.get(segs.size()-1);
>> Set<String> ids = p.getMatrixParams().keySet();
>> // ids contains the rest of the ids (234, 345 above)
>> return ...
>> }
>>
>> Marc.
>>
>> ---
>> Marc Hadley <marc.hadley at sun.com>
>> CTO Office, Sun Microsystems.
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>

---
Marc Hadley <marc.hadley at sun.com>
CTO Office, Sun Microsystems.