users@jersey.java.net

Re: [Jersey] Example of matrix URIs?

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Tue, 11 Nov 2008 09:21:11 +0100

On Nov 10, 2008, at 10:40 PM, Gili wrote:

>
>
> Paul Sandoz wrote:
>>
>> A better use would be:
>>
>> http://example.com/images;1;2;5/tags
>>
>> as then the matrix parameters are associated with a named path
>> segment. Matrix parameters are ignored when path matching. The best
>> way to think about them is as query parameters scoped to a path
>> segment.
>>
>
> I don't understand how the above would work. Are you saying that
> "images"
> would have 3 matrix parameters: 1, 2 and 5?

Yes, specifically there are three matrix parameter names, each of
which has no value.


> Paul Sandoz wrote:

>>
>> If you are using the path segment matching approach you could do:
>>
>> @GET_at_Path("images/{id: <regex for digits and commas>}/tags")
>> public ... get(@PathParam("id") ImageList images) { ... }
>>
>> and the class ImageList has a string constructor that parses the
>> comma
>> separated list of names (see the Sparklines sample [1]).
>>
>
> Okay, I just wanted to get back a List<Integer>. I guess I could
> code this
> up myself.

You could only do that with @MatrixParam (or @QueryParam) if you have
one or more matrix parameters with the same name. You can use the
following class for any @*Param value:

   public class IntegerList extends ArrayList<Integer> {
       public IntegerList(String s) {
           super();

           for (String v : s.split(",")) {
               try {
                   add(Integer.parseInt(v.trim()));
               } catch (Exception ex) {
                   throw new WebApplicationException(400);
               }
           }
           if (isEmpty())
               throw new WebApplicationException(400);
       }
   }

Paul.