users@jersey.java.net

[Jersey] Re: Adding grammar to the resources

From: Tatu Saloranta <tsaloranta_at_gmail.com>
Date: Thu, 1 Aug 2013 11:16:06 -0700

On Wed, Jul 31, 2013 at 5:34 PM, Nick Khamis <symack_at_gmail.com> wrote:

> On Tue, Jul 30, 2013 at 7:39 AM, Jakub Podlesak <jakub.podlesak_at_oracle.com
> > wrote:
>
>> Hi Nick,
>>
>> Cam is right, JAX-RS dispatch algorithm could not tell which
>> resource method to invoke.
>>
>> What you can do instead is something like:
>>
>> @GET
>> @Produces({MediaType.APPLICATION_JSON})
>> public List<Wine> findByName(@QueryParam("name") String name) {
>> if (name != null) {
>> return findByName(name);
>> } else {
>> return findAll();
>> }
>>
>> // removed JAX-RS annotations
>> public List<Wine> findAll() {…}
>>
>> @Path("{id}")
>> @GET
>> @Produces(MediaType.APPLICATION_JSON)
>> public Wine findById(@PathParam("id") String id) {
>> …
>> }
>>
>> HTH,
>>
>> ~Jakub
>>
>>
>>
> Hello Guys,
>
> Thank you so much for your response. Not sure how elegant this hack would
> be as we keep adding search
> criteria, (ie, search by type, region, date, etc...). Is there a better
> solution? The bug in the dispatching algorithm
> is a WONTFIX?
>
>
I think it is rather that as per JAX-RS (and, I would argue, most all
frameworks) routing by query parameter is something that should not be
done. There are many practical reasons for this, including the fact that
whereas path is hierarchic, and lends itself to relatively easy mapping,
and state machines, query parameter set is an unordered bag of things. And
operating on such dynamic, heterogenous data is difficult from otherwise
static handler.

So really it's better to leave that to either app developer, or to another
framework/library/abstraction.

That is my reading anyway: I am not involved in JAX-RS or Jersey
development, so this is just my opinion.

-+ Tatu +-