具有不同数量查询参数的两种 GET 方法:REST [英] Two GET methods with different number of query parameters : REST

查看:51
本文介绍了具有不同数量查询参数的两种 GET 方法:REST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Rest 端点类,如下所示:

I have an Rest endpoint class as below :

@Path("/sports")
public interface SportsEndpoint {

    @GET
    List<Player> getPlayersOfSports(@QueryParam("sportId") String sportId, @QueryParam("sportName") String sportName);


    @GET
    List<Player> getPlayersOfSports(@QueryParam("sportId") String sportId, @QueryParam("sportName") String sportName, @QueryParam("country") String country);

}

如您所见,我有两个 GET 方法,它们具有几乎相同的签名,但第二个方法需要一个额外的 QueryParam.

As you can see, I have two GET methods which has almost the same signature except the second method takes an additional QueryParam.

我尝试使用以下网址访问这些端点:

I try to access these endpoints using the urls:

http://localhost:8080/rest/api/sports?sportId=100&sportName=badmintonhttp://localhost:8080/rest/api/sports?sportId=100&sportName=badminton&country=japan

这两个 url 都解析为使用第一个方法签名.理想情况下,我期望对于第一个 url,将调用第一个方法签名,对于第二个 url,将调用第二个方法(具有 3 个查询参数的方法).
但看起来在这两种情况下,第一种方法都被调用了.

Both these urls are resolving to use the first method signature. Ideally, I was expecting that for the first url, the first method signature would be called and for the second url the second method(the one with 3 query parameters) would have been called.
But looks like in both cases the first method is getting called.

我知道 Rest 资源由路径而不是查询参数唯一标识.但是,即使查询参数的数量不同,其余端点也不会被唯一标识吗?
有人可以指点我一些规范/文章/文档,在这些规范/文章/文档中,我可以在设计 rest api 端点时理解多态性吗?

I know that Rest resources are uniquely identified by the path and not the query parameters. But is it also true that even though the number of query parameters are different, the rest endpoint will not be uniquely identified ?
Can someone point me to some specifications/articles/documentation where I can understand polymorphism when it comes to designing rest api endpoints ?

仅供参考:我正在使用 RestEasy.

FYI: I am using RestEasy.

推荐答案

QueryParams 是可选参数

QueryParams are the optional params as

你说得对,我知道 Rest 资源是独一无二的由路径而不是查询参数标识

you correctly stated I know that Rest resources are uniquely identified by the path and not the query params

并回答您的问题,

是的,即使查询参数的数量不同,其余的端点不会被唯一标识?因为它是由路径不是查询参数.

yes even though the number of query parameters are different, the rest endpoint will not be uniquely identified ? as it is identified by the path not the query params.

如果您希望根据 querystring 中的输入数量调用您的方法最好创建一个方法,它根据您的要求获取所有 queryparam 并根据提供的输入数量,分别调用每个方法.

If you want that your methods should be called based on the no of inputs in your querystring it is better to create a single method, which takes all the queryparam according to your requirement and based on the no of inputs provided, you call each methods separately.

所以在你的情况下,它可以重写如下:-

So in your case, it can be rewritten as below :-

    @Path("/sports")
    public interface SportsEndpoint {

        @GET
        List<Player> getPlayers(@QueryParam("sportId") String sportId, @QueryParam("sportName") String sportName , @QueryParam("country") String country){
if(sportId ! =null && sportName!=null)
{
getPlayersOfSports(sportId,sportName);
}
else if (sportId ! =null && sportName!=null && country!=null)
{
getPlayersOfSports(sportId,sportName,country);
}
}

还有

然后在业务逻辑中定义方法重载,即将有 2 个重载方法,根据参数的数量而有所不同.这样你就可以实现多态.

then you define the method overloading in your business logic, which will have 2 overloaded methods, which varies based on no of arguments. This way you can achieve polymorphism.

这篇关于具有不同数量查询参数的两种 GET 方法:REST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆