14

Could we create the same GET URI but with different query parameters?

For example, I have two REST GET URIs:

/questions/ask/?type=rest
/questions/ask/?byUser=john

Now the REST service is not recognizing two GET methods as separate and considering it only 1 GET method which is declared as first.

  1. Why is it behaving this way?
  2. Is there any way that I could make two GET methods having different Query Parameters?

It would be highly appreciated if you could quote any resource.

Federico Grandi
  • 6,785
  • 5
  • 30
  • 50
ritesh
  • 907
  • 3
  • 11
  • 31

4 Answers4

27

Because a resource is uniquely identified by its PATH (and not by its params). Two resources you define have the same PATH.

@Path("/questions/ask")

According to JSR-311 spec:

Such methods, known as sub-resource methods, are treated like a normal resource method (see section 3.3) except the method is only invoked for request URIs that match a URI template created by concatenating the URI template of the resource class with the URI template of the method.

Since your data model includes two distinct resources I suggest making two rest methods with different paths:

@Path("/questions/ask/type")
@Path("/questions/ask/user")

This is the RESTful way, since one URI represents one and only one resource and there should be no overloading. If one URI represents more than one resource that means you got it wrong somewhere.

darijan
  • 9,725
  • 25
  • 38
  • is this also true when you have different number of query params in the methods ? I mean, if I have two rest methods annotated with `@GET` with almost same signature except that the first method takes 2 query params, and the second method takes 3 query params. So even if I acll the rest endpoint with 3 params, the first method will only get called ? – RITZ XAVI Mar 02 '17 at 15:38
6

You cannot overload REST requests.

In your business layer you would have to check which of the two variables are set and then you will have to do the required processing.

kryger
  • 12,906
  • 8
  • 44
  • 65
JHS
  • 7,761
  • 2
  • 29
  • 53
  • could you please explain more about this statement: "In your business layer you would have to check which of the "two variables" are set and then you will have to do the required processing." – ritesh Jun 24 '13 at 07:57
  • Your REST request should have `type` and `byUser`. You could have a `DefaultValue`. I assume you are calling different method or doing different processing depending of what is set. In my business layer I check which of the two is set and call that specific method. OR you can go for two different REST URI's. – JHS Jun 24 '13 at 08:00
  • I am creating two methods say getQuestionByType(@QueryParam("type")) and getQuestionByUser(@QueryParam("byUser")). So in business layer how would you check which of the two is set and call that specific method? – ritesh Jun 24 '13 at 08:04
  • One `REST` `GET` method with 2 parameters. Then in the business layer check whether `type` is set or `byUser` is set. And accordingly do the needful. – JHS Jun 24 '13 at 08:09
  • Thanks Junaid, I guess as Sanjaya has mentioned. – ritesh Jun 24 '13 at 08:11
5

You can not have two getters with same uri but different request parameters. What you can do is to have one getter method with many request parameters.

@RequestMapping(value = "uri", method = RequestMethod.GET)
public String test(@RequestParam String type, @RequestParam String byUser) 

then call it with two parameters

/questions/ask/?type=rest&byUser=john

You have to handle the logic inside test method to handle these parameters accordingly.

Regarding Darijan, I think that it is up to to decide to go with two methods or one method considering what the underline logic is. If you are going with 2 methods then use two uri. If the business logic is ok to go with one uri then use the way I answered

Sanjaya Liyanage
  • 4,706
  • 9
  • 36
  • 50
  • What if I want to pass any combination of these two parameters, say type only,byUser only, type and byUser as you mentioned then it will work,right? – ritesh Jun 24 '13 at 08:10
  • It will, you just check whether the other parameters is equal to `null`. But that's not "the REST way". – darijan Jun 24 '13 at 08:11
  • Thnaks darijan and Sanjaya ! but tell me a way to accept two answers. darijan has answered one part of question and Sanjaya & Junaid has answered other part. Please help me out. :) – ritesh Jun 24 '13 at 08:14
  • Not sure you can. Give accept to Sanjaya, he has the least reputation of us all :) – darijan Jun 24 '13 at 08:15
  • @darijan It will not. Because required value is default false. If you want to do so then set (@RequestParam(required = false) String type, @RequestParam(required = false) String byUser) then it will work – Sanjaya Liyanage Jun 24 '13 at 08:17
  • darijan I will look for perfect answer for 1 more day. One requirement is still missing. ;) – ritesh Jun 24 '13 at 08:18
  • @Sanjaya Liyanage: I think the code is applicable with Spring framework, not with resteasy. Correct me if I am wrong. – ritesh Jun 24 '13 at 08:53
  • 1
    @Ritesh I am not much familiar with RestEasy but I think you can not directly use the code as RestEasy does not suppory RequestParam .But you can use QueryParam instead RequestParam in restEasy and still code should work. Perhaps you have to change RequestMapping(value = "uri", method = RequestMethod.GET) part also according to restEasy. – Sanjaya Liyanage Jun 24 '13 at 09:03
2

You can overload the rest endpoint in termsof what request/query parameters are present in the request. Here's the answer that solved my use case: create two method for same url pattern with different arguments

Community
  • 1
  • 1
arshellium
  • 215
  • 1
  • 6
  • 17
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes – afxentios Jan 24 '17 at 07:38