users@jersey.java.net

Re: [Jersey] Example of matrix URIs?

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Tue, 11 Nov 2008 19:48:26 +0100

On Nov 11, 2008, at 7:31 PM, Gili wrote:

>
> Sorry, I forgot to mention... For:
>
> http://example.com/images/1;2;5?select=random
>
> I already have a sub-resource that matches @Path("{id}").

For what path segment the image ids "1;2;5" ?

> This means that I
> want one method returning a sub-resource (the "Image" resource), and
> another
> method returning an XML document (the list of random images). In other
> words, the method return-type varies based on the query string.
>
> Is there a way for me to configure JAX-RS to pick the right method
> somehow?
> What do you recommend?
>

I recommend you use a comma separated list of image ids as it works
better for path matching. You could have "random" as part of the path
too:

@Path("{images: [\d,]+"}
public class ImagesResource {
    @PathParam("images") IntegerList images;

    @Path("random")
    @GET
    public ... get() {
    }

   @GET
}

If you use a query parameter then you have to do some checking of the
values yourself.

It would help me if you could present some code of how you are
componentizing things as i might be able to help you better. Or break
down and present the different URIs you want to support.

Paul.

> Thanks,
> Gili
>
>
>
> Gili wrote:
>>
>> Paul,
>>
>> Does @Path ignore query arguments? If I want to parse for:
>>
>> http://example.com/images/1;2;5?select=random
>>
>> Can I use the following?
>>
>> @Path("{id}?select=random")
>>
>> or would I be forced to use a single method and fork execution myself
>> based on the query arguments?
>>
>> Thanks,
>> Gili
>>
>>
>> Paul Sandoz wrote:
>>>
>>>
>>> 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.
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>>
>>>
>>>
>>
>>
>
> --
> View this message in context: http://n2.nabble.com/Example-of-matrix-URIs--tp1482069p1486308.html
> Sent from the Jersey mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>