我不是在问这里已经问过的问题:
@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,或者当数据不是分层时实现一个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是最好的。
这要看情况……
@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是最好的。
这要看情况……