我不是在问这里已经问过的问题: @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来区分他们的信息,就像我上面举例的那样。我也很想知道这种做法背后的原因。


我个人使用的方法是“如果对用户来说书签包含这些参数的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.

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

可以使用查询参数进行过滤,使用路径参数进行分组。下面的链接有关于何时使用pathParams或QueryParams的很好的信息

这就是我的工作。

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

GET /employee/{id}

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

GET /employee?start=1&size=10

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

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

REST本身可能不是一个标准,但是阅读一般的REST文档和博客文章应该会给你一些指导,让你更好地构造API url。大多数其他api倾向于在路径中只有资源名称和资源id。如:

/departments/{dept}/employees/{id}

一些REST api使用查询字符串进行过滤,分页和排序,但由于REST不是一个严格的标准,我建议检查一些REST api,如github和stackoverflow,看看什么可以很好地为您的用例工作。

我建议在路径中放入任何必需的参数,而任何可选参数都应该是查询字符串参数。当尝试编写匹配不同组合的URL处理程序时,在路径中放置可选参数最终会变得非常混乱。

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

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

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

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

这里列出了更多的采用者

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

我给一个例子来理解我们什么时候使用@Queryparam和@pathparam

例如,我正在使用一个资源是carResource类

如果你想让你的资源方法的输入是强制性的,那么使用参数类型为@pathaparam,如果你的资源方法的输入应该是可选的,那么保持参数类型为@QueryParam

@Path("/car")
class CarResource
{
    @Get
    @produces("text/plain")
    @Path("/search/{carmodel}")
    public String getCarSearch(@PathParam("carmodel")String model,@QueryParam("carcolor")String color) {
        //logic for getting cars based on carmodel and color
            -----
        return cars
    }
}

为这个资源传递请求

req uri ://address:2020/carWeb/car/search/swift?carcolor=red

如果你给出这样的要求,资源将给出基于汽车的型号和颜色

 req uri://address:2020/carWeb/car/search/swift

如果你给出这样的要求,资源方法将只显示基于快速模型的汽车

req://address:2020/carWeb/car/search?carcolor=red

如果你这样给出,我们会得到ResourceNotFound异常因为在car资源类中,我声明了carmodel为@pathPram也就是说,你必须并且应该将carmodel作为reQ uri,否则它不会将reQ传递给resource但如果你不传递颜色它也会将reQ传递给resource为什么呢因为颜色是@quetyParam,这在reQ中是可选的。

这是个很有趣的问题。

你可以同时使用它们,关于这个主题没有任何严格的规则,但是使用URI路径变量有一些优点:

缓存: 大多数互联网上的web缓存服务不缓存GET请求时,他们包含查询参数。 他们这样做是因为有很多RPC系统使用GET请求来更改服务器中的数据(失败!!)Get必须是一个安全的方法)

但是如果使用路径变量,所有这些服务都可以缓存GET请求。

层次结构: 路径变量可以表示层次结构: /城市/街道/的地方

它为用户提供了关于数据结构的更多信息。

但如果你的数据没有任何层次关系,你仍然可以使用Path变量,使用逗号或分号:

/城市/经度,纬度

作为规则,当参数的顺序重要时使用逗号,当顺序不重要时使用分号:

/ IconGenerator /红色,蓝色,绿色

除了这些原因之外,在某些情况下,使用查询字符串变量是非常常见的:

当您需要浏览器自动将HTML表单变量放入URI时 当你处理算法的时候。例如谷歌引擎使用查询字符串:

http:// www.google.com/search ? q =休息

总而言之,没有任何强烈的理由使用这种方法,但只要可以,就使用URI变量。

@QueryParam可以方便地与默认值注释一起使用,这样如果没有传递查询参数,就可以避免空指针异常。

当您希望解析来自GET请求的查询参数时,您可以简单地为处理GET请求的方法定义相应的参数,并使用@QueryParam注释对它们进行注释

@PathParam提取URI值并与@Path匹配。从而得到输入参数。 @PathParam可以有多个,并且设置为方法参数 @ path(" /休息”) 公共类Abc { @ get @ path(" /味精/ {p0} / {p1}”) 与@ (" text / plain”) public String add(@PathParam("p0") Integer param1, @PathParam("p1") Integer param2) { 返回String.valueOf (param1 + param2); } }

在上面的例子中, http://localhost: 8080 / Restr /休息/味精/ {p0} / {p1}, P0匹配param1 p1匹配param2。对于URI来说 http://localhost: 8080 / Restr /休息/味精/ 4/6, 结果是10。

在REST服务中,JAX-RS提供了@QueryParam和@FormParam来接受来自HTTP请求的数据。HTTP表单可以通过不同的方法提交,比如GET和POST。

@QueryParam:接受GET请求并从查询字符串中读取数据。

@FormParam:接受POST请求并从HTML表单或媒体的任何请求中获取数据

来自维基百科:统一资源定位器

包含数据的路径,通常以分层形式组织,显示为由斜杠分隔的段序列。

可选查询,以问号(?)与前一部分隔开,包含非分层数据的查询字符串。

-根据URL的概念设计,我们可以为分层数据/指令/定位器组件实现一个PathParam,或者当数据不是分层时实现一个QueryParam。这是有意义的,因为路径是自然有序的,而查询包含的变量可能是任意有序的(无序的变量/值对)。

之前的一位评论者写道,

我认为,如果参数标识一个特定的实体,您应该使用路径变量。

另一个写道:

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

另一个,

我建议在路径中放入任何必需的参数,而任何可选参数都应该是查询字符串参数。

— However, one might implement a flexible, non-hierarchical system for identifying specific entities! One might have multiple unique indexes on an SQL table, and allow entities to be identified using any combination of fields that comprise a unique index! Different combinations (perhaps also ordered differently), might be used for links from various related entities (referrers). In this case, we might be dealing with non-hierarchical data, used to identify individual entities — or in other cases, might only specify certain variables/fields — certain components of unique indexes — and retrieve a list/set of records. In such cases, it might be easier, more logical and reasonable to implement the URLs as QueryParams!

Could a long hexadecimal string dilute/diminish the value of keywords in the rest of the path? It might be worth considering the potential SEO implications of placing variables/values in the path, or in the query, and the human-interface implications of whether we want users to be able to traverse/explore the hierarchy of URLs by editing the contents of the address bar. My 404 Not Found page uses SSI variables to automatically redirect broken URLs to their parent! Search robots might also traverse the path hierarchy. On the other hand, personally, when I share URLs on social media, I manually strip out any private unique identifiers — typically by truncating the query from the URL, leaving only the path: in this case, there is some utility in placing unique identifiers in the path rather than in the query. Whether we want to facilitate the use of path components as a crude user-interface, perhaps depends on whether the data/components are human-readable or not. The question of human-readability relates somewhat to the question of hierarchy: often, data that may be expressed as human-readable keywords are also hierarchical; while hierarchical data may often be expressed as human-readable keywords. (Search engines themselves might be defined as augmenting the use of URLs as a user-interface.) Hierarchies of keywords or directives might not be strictly ordered, but they are usually close enough that we can cover alternative cases in the path, and label one option as the "canonical" case.

对于每个请求的URL,我们可能会回答几种基本的问题:

我们要求/提供什么样的记录/东西? 我们对哪一个感兴趣? 我们希望如何呈现信息/记录?

Q1几乎肯定最好由路径或PathParams覆盖。 Q3(可能通过一组任意顺序的可选参数和默认值进行控制);几乎可以肯定QueryParams是最好的。 这要看情况……

您可以同时支持查询参数和路径参数,例如,在资源聚合的情况下——当子资源的集合本身有意义时。

/departments/{id}/employees
/employees?dept=id

查询参数可支持分层和非分层子集设置;路径参数仅分级。

资源可以显示多个层次结构。如果要查询跨层次边界的广泛子集合,则支持短路径。

/inventory?make=toyota&model=corolla
/inventory?year=2014

使用查询参数组合正交层次结构。

/inventory/makes/toyota/models/corolla?year=2014
/inventory/years/2014?make=toyota&model=corolla
/inventory?make=toyota&model=corolla&year=2014

在组合的情况下只使用路径参数——当一个资源脱离了它的父元素就没有意义了,而且所有子元素的全局集合本身并不是一个有用的资源。

/words/{id}/definitions
/definitions?word=id   // not useful

简而言之,

@Pathparam适用于通过资源和查询字符串传递的值

/用户/1 /user?id=1

@Queryparam只适用于传递查询字符串的值

/user?id=1

在讨论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

对于资源名称和id,我使用@PathParams。对于可选变量,我使用@QueryParams

根据我的理解:

使用@PathParam -当它是一个强制性的项目,如Id 获得/气球/ {id} 使用@QueryParam -当你有确切的资源,但需要过滤一些可选的特征,如颜色,大小等。 得到/气球/ 123 ?颜色= red&size =

路径参数- 路径参数是URL路径中的一个变量,帮助指向一些特定的资源。

Example - https://sitename.com/questions/115

这里,如果115是一个路径参数,它可以用其他有效的数字来改变,以获取/指向同一应用程序上的其他一些资源。

查询参数- 查询参数是URL路径中的变量,用于从列表中过滤某些特定的资源。

Example - https://sitename.com/questions/115?qp1=val1&qp2=val2&qp3=val3

其中qp1、qp2和qp3为查询变量,其值分别为val1、val2和val3。在获取/保存数据时,这些可以用作过滤器。查询变量总是在URL中以问号(?)结尾。

我喜欢以下几点:

@PathParam

当需要参数时,例如ID, productNo .

GET /user/details/{ID}
GET /products/{company}/{productNo}

@QueryParam

当您需要传递可选参数,如过滤器,在线状态和他们可以为空

GET /user/list?country=USA&status=online
GET /products/list?sort=ASC 

同时使用时

GET /products/{company}/list?sort=ASC