Two GET methods with different query parameters

前端 未结 4 559
星月不相逢
星月不相逢 2020-12-31 10:19

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         


        
4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-31 11:10

    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

提交回复
热议问题