Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overload path functions with different @QueryParams

I would like to have multiple functions for the same GET Path.

As well i would like my web service to "find" these functions if and only if the query parameters match the parameters in the URL String.

For example:

I have the Path("/myGET")

And for that path i would like to have 2 functions:

@GET  
@Produces(MediaType.APPLICATION_JSON)
@TypeHint(TagSets.class)
public Response getTagSets(@QueryParam("entityId") Integer entityId)
{
    ...
}

And

@GET  
@Produces(MediaType.APPLICATION_JSON)
@TypeHint(TagSets.class)
public Response getTagSets(){
    ...
}

Right now i am getting an error:

SEVERE: The following errors and warnings have been detected with resource and/or provider classes: SEVERE: Producing media type conflict. The resource methods public javax.ws.rs.core.Response<...>.getTagSets(java.lang.Integer) and public javax.ws.rs.core.Response<...>.getTagSets(java.lang.Integer,java.lang.Integer) can produce the same media type SEVERE: Producing media type conflict. The resource methods public javax.ws.rs.core.Response<...>.getTagSets() and public javax.ws.rs.core.Response <...>.getTagSets(java.lang.Integer,java.lang.Integer) can produce the same media type

So first: Is there any way to achieve what i want to do here..

second: If this is available, is there any way that a path will be found if and only if the query parameters match exactly what is requested in the function? for example if the same path will be called with @QueryParam("differentParam") it will not reach any of the 2 functions.

Third: If there is no way to do this with Jersey is there a way to do it with any other frame work?.

IMPORTANT: As people that answer the question think that i am looking for a work around and not a solution. today i am using 1 function and checking the parameters and invoking what i need from this (that is what i used before i posted the question). But what i am looking for is maybe using the frameworks power to save me the trouble

Thanks.

like image 488
Gleeb Avatar asked Aug 27 '13 12:08

Gleeb


1 Answers

A resource is uniquely defined by its path, and not by its params. Two resources you defined have the same path. You can either define new paths for each of them like /myGet/entity, /myGet/, /myGet/differentParam; or use a single path as /myGet/ and check the query params as following:

@GET  
@Produces(MediaType.APPLICATION_JSON)
@TypeHint(TagSets.class)
public Response getTagSets(@Context HttpServletRequest request){

       ...

       if (request.getParameterMap().isEmpty()) {
           // then you have no query params, implement as there are no query params
       } else {
           String queryParam = request.getQueryString();
           // check queryParam, and add another if else statements, implement
       }

       ...

}
like image 51
anvarik Avatar answered Sep 21 '22 05:09

anvarik