0

I would like to have two GET methods on my Rest resource class. one would react if query param has value1 and second on value2

Lets say:

@Path("/myApi")
public class MyApiService {

     @GET
     @Produces(MediaType.APPLICATION_JSON)
     public Response methodOne(...) {
         ...
         return ...;
     }

     @GET
     @Produces(MediaType.APPLICATION_JSON)
     public Response methodTwo(...) {
         ...
         return ...;   
     }

How to achieve conditional routing for query params

I would like to methodOne() reacts if QueryParam is ?type=one and methodTwo() if QueryParam is ?type=two

To Kra
  • 3,344
  • 3
  • 38
  • 45
  • Would you please tell us why you think this is necessary? –  Apr 08 '15 at 13:32
  • One option is to use [Sub-Resource Locators](http://stackoverflow.com/a/28978111/2587435) – Paul Samsotha Apr 08 '15 at 13:47
  • @Tichodroma I would like to separate methods, so in for example in swagger you could see more methods. I think methods per different query param isnt so stupid as it looks like... – To Kra Apr 09 '15 at 14:43

2 Answers2

0

Choosing servlet handlers based on QueryParam is not a good aproach, and by default no library gives you oportunity to do so. The closest that comes to mind is PathParam, that is something like Path("\api\{param1}\{param2}") but it's not what you are looking for.

To achieve want your want just

  • unregister those methods as servlet handlers (Optional, if you don't need them outside of queryparam selection scope)
  • define a new one that will choose based on query param

@GET @Produces(MediaType.APPLICATION_JSON)

public Response methodThree(QueryParam('type') String type) {
return type.equals("type1") ? this.methodOne() : this.methodTwo();   
}
Eugen Halca
  • 1,775
  • 2
  • 13
  • 26
0

You cannot have two methods with identical parameters for the same path. It's not pretty, but it will work..

@GET
@Produces(MediaType.APPLICATION_JSON)
public Response myMethod(@QueryParam("type") String type){

 if(type.equals("one"))
     return methodOne();
 else
     return methodTwo();
}
azraelAT
  • 752
  • 5
  • 9