gpt4 book ai didi

java - 查询参数数量不同的两种 GET 方法 : REST

转载 作者:行者123 更新时间:2023-12-04 12:06:57 24 4
gpt4 key购买 nike

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

@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 .

我尝试使用 url 访问这些端点:
http://localhost:8080/rest/api/sports?sportId=100&sportName=badminton http://localhost:8080/rest/api/sports?sportId=100&sportName=badminton&country=japan
这两个 url 都解析为使用第一个方法签名。理想情况下,我期望对于第一个 url,将调用第一个方法签名,而对于第二个 url,将调用第二个方法(具有 3 个查询参数的方法)。
但看起来在这两种情况下,第一种方法都被调用了。

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

仅供引用:我正在使用 RestEasy。

最佳答案

据我所知,Java rest API 是 Jax-R 的实现,因此您可以阅读 Jax-Rs specification ,
Jax-Rs 将路径映射到资源方法。资源方法必须有唯一的路径。查询参数不被视为路径的一部分。资源方法是用请求方法指示符注释的资源类的方法。当两个方法共享相同的指示符和路径时,规范并没有决定会发生什么,但是大多数实现将只使用其中之一并且不会报告错误。
(符合 URL specs 。)
这意味着您不能使用重载。
允许继承。来自 Jax-Rs 规范:

JAX-RS annotations MAY be used on the methods and method parameters of a super-class or an implemented interface. Such annotations are inherited by a corresponding sub-class or implementation class method provided that method and its parameters do not have any JAX-RS annotations of its own. Annotations on a super-class take precedence over those on an implemented interface. If a subclass or implementation method has any JAX-RS annotations then all of the annotations on the super class or interface method are ignored. E.g.:

关于java - 查询参数数量不同的两种 GET 方法 : REST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42559180/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com