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.
It would be highly appreciated if you could quote any resource.
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 "&".
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With