我不是在问这里已经问过的问题: @PathParam和@QueryParam有什么区别

这是一个“最佳实践”或惯例问题。

什么时候使用@PathParam vs @QueryParam。

我能想到的是,这个决定可能是用这两者来区分信息模式。让我在下面说明我的LTPO -不完美的观察。

PathParam的使用可以保留在信息类别中,这将很好地归入信息树的一个分支。PathParam可以用于下钻到实体类层次结构。

然而,QueryParam可以保留用于指定属性以定位类的实例。

例如,

/ Vehicle /车?车主= 123 / House /克?该区域= newengland

(category) ?instance

@GET
@Path("/employee/{dept}")
Patient getEmployee(@PathParam("dept")Long dept, @QueryParam("id")Long id) ;

vs / category (instance)

@GET
@Path("/employee/{dept}/{id}")
Patient getEmployee(@PathParam("dept")Long dept, @PathParam("id")Long id) ;

vs ?类别+实例

@GET
@Path("/employee")
Patient getEmployee(@QueryParam("dept")Long dept, @QueryParam("id")Long id) ;

我不认为这样做有一个标准的惯例。是吗?然而,我想知道人们如何使用PathParam和QueryParam来区分他们的信息,就像我上面举例的那样。我也很想知道这种做法背后的原因。


当前回答

这就是我的工作。

如果存在基于id检索记录的场景,例如您需要获取id为15的员工的详细信息,那么您可以使用@PathParam资源。

GET /employee/{id}

如果需要获得所有员工的详细信息,但每次只需要10个,则可以使用query param

GET /employee?start=1&size=10

这表示,起始雇员id 1获得10条记录。

总之,使用@PathParam进行基于id的检索。用户@QueryParam用于过滤器,或者如果你有任何用户可以传递的固定选项列表。

其他回答

我认为,如果参数标识一个特定的实体,您应该使用路径变量。例如,为了获得我所请求的博客上的所有帖子

GET: myserver.com/myblog/posts

要获得id = 123的职位,我会请求

GET: myserver.com/myblog/posts/123

但要过滤我的帖子列表,并获得2013年1月1日以来的所有帖子,我会要求

GET: myserver.com/myblog/posts?since=2013-01-01

在第一个例子中,“posts”标识了一个特定的实体(博客文章的整个集合)。在第二个例子中,“123”也表示一个特定的实体(一篇博客文章)。但是在最后一个例子中,参数“since=2013-01-01”是一个过滤posts集合的请求,而不是一个特定的实体。分页和排序是另一个很好的例子。

GET: myserver.com/myblog/posts?page=2&order=backward

希望这能有所帮助。:-)

在讨论QueryParam和PathParam之前。让我们首先了解URL及其组件。URL由端点+资源+ queryParam/ PathParam组成。

例如,

URL: https://app.orderservice.com/order?order=12345678

or

URL: https://app.orderservice.com/orders/12345678

在哪里

端点:https://app.orderservice.com

资源:订单

queryParam:点= 12345678

PathParam: 12345678

@QueryParam:

当需求是基于特定的条件/标准筛选请求时,使用QueryParam。标准是用?URL中资源的后面。queryParam中可以使用&符号指定多个过滤标准。

例如:

https://app.orderser.com/orders?order=12345678 &客户名=X

@PathParam:

当需要基于guid/id选择特定的顺序时,使用PathParam。PathParam是URL中资源的一部分。

例如:

https://app.orderservice.com/orders/12345678

我个人使用的方法是“如果对用户来说书签包含这些参数的url是有意义的,那么使用PathParam”。

For instance, if the URL for a user profile includes some profile id parameter, since this can be bookmarked by the user and/or emailed around, I would include that profile id as a path parameter. Also, another considerent to this is that the page denoted by the URL which includes the path param doesn't change -- the user will set up his/her profile, save it, and then unlikely to change that much from there on; this means webcrawlers/search engines/browsers/etc can cache this page nicely based on the path.

If a parameter passed in the URL is likely to change the page layout/content then I'd use that as a queryparam. For instance, if the profile URL supports a parameter which specifies whether to show the user email or not, I would consider that to be a query param. (I know, arguably, you could say that the &noemail=1 or whatever parameter it is can be used as a path param and generates 2 separate pages -- one with the email on it, one without it -- but logically that's not the case: it is still the same page with or without certain attributes shown.

希望这能有所帮助——我很感激这个解释可能有点模糊:)

原因其实很简单。当使用查询参数时,你可以输入字符,如“/”,你的客户端不需要对它们进行html编码。还有其他原因,但这只是一个简单的例子。至于何时使用路径变量。我想说的是,当你处理id或者路径变量是一个查询的方向时。

正如席恩所指出的,REST不是一个标准。但是,如果您希望实现基于标准的URI约定,则可以考虑oData URI约定。Ver 4已被批准为OASIS标准,并且存在用于各种语言的oData库,包括通过Apache Olingo的Java。不要因为它是微软的产物而放弃它,因为它也获得了其他行业参与者的支持,包括红帽、思杰、IBM、黑莓、Drupal、Netflix Facebook和SAP

这里列出了更多的采用者