Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two GET methods with different query parameters

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.

like image 622
ritesh Avatar asked Jun 24 '13 07:06

ritesh


People also ask

How do I pass multiple parameters in GET request URL?

Any word after the question mark (?) in a URL is considered to be a parameter which can hold values. The value for the corresponding parameter is given after the symbol "equals" (=). Multiple parameters can be passed through the URL by separating them with multiple "&".

CAN GET method have query parameters?

When the GET request method is used, if a client uses the HTTP protocol on a web server to request a certain resource, the client sends the server certain GET parameters through the requested URL. These parameters are pairs of names and their corresponding values, so-called name-value pairs.

What is the difference between QueryParam and FormParam?

An HTTP form can be submitted by different methods like GET and POST. @QueryParam : Accepts GET request and reads data from query string. EX. @FormParam: Accepts POST request and fetches data from HTML form or any request of the media type application/x-www-form-urlencoded.

How do you add parameters in GET request?

To do http get request with parameters in Angular, we can make use of params options argument in HttpClient. get() method. Using the params property we can pass parameters to the HTTP get request. Either we can pass HttpParams or an object which contains key value pairs of parameters.


1 Answers

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.

like image 131
darijan Avatar answered Nov 15 '22 16:11

darijan