Two GET methods with different query parameters

本小妞迷上赌 提交于 2019-11-30 09:06: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.

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

JHS

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.

arshellium

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!