Two GET methods with different request parameters: REST

Is it possible to create the same GET URI, but with different request parameters?

For example: I have two REST GET URIs:

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

Now the REST service does not recognize two GET methods as a separate one and treats only one GET method declared as the first.

  • why does he behave this way?
  • Is there a way to make two GET methods different request parameters?

It would be very helpful if you could specify any resource.

+8
java rest resteasy
source share
4 answers

Because the resource is uniquely identified by its PATH (and not its parameters). The two resources you define have the same PATH.

 @Path("/questions/ask") 

According to JSR-311 specification :

Such methods, known as subresource methods, are considered as normal resource methods (see section 3.3), except that the method is called only for query URIs that match the URI pattern created by concatenating the resource class URI pattern with the Method URI pattern .

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

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

This is a RESTful path, since one URI represents one and only one resource, and there should be no overload. If one URI represents more than one resource, it means that you were mistaken somewhere.

+16
source share

You cannot have two getters with the same uri, but with different request parameters. What you can do is 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 must process the logic inside the test method to properly process these parameters.

As for Darijan, I think he decided to solve two methods or one method, taking into account what is the main line. If you are going to use 2 methods, use two uri. If business logic is ok to go with one uri then use the way I answered

+5
source share

You cannot request Overload REST .

At your business level, you will need to check which of the two variables is set, and then you will need to perform the necessary processing.

+3
source share

You can overload the rest of the endpoint in terms of what request / request parameters are present in the request. Here is the answer that my use case solved: create two methods for the same url template with different arguments

0
source share

All Articles