【发布时间】:2018-05-17 06:53:18
【问题描述】:
我不是在问What is the difference between @PathParam and @QueryParam 已经问过的问题
这个问题与围绕 restful 约定的“最佳实践”有关。
我有一个包含以下字段的问题资源。
[
{
"questionId":6,
"area":"TECHNICAL",
"title":"Find the index of first 1 in an infinite sorted array of 0s and 1s",
"description":"Given an infinite sorted array consisting 0s and 1s. The problem is to find the index of first 1 in that array. As the array is infinite, therefore it is guaranteed that number 1 will be present in the array.",
"state":"ACTIVE",
"difficultyLevel":"EASY",
"skills":[
{
"skillId":1,
"skillName":"ALGORITHM"
},
{
"skillId":2,
"skillName":"PROGRAMMING"
}
],
"proposedBy":"agrawalo",
"noOfTimesUsed":0,
"examples":null,
"probes":null,
"approvedBy":null,
"addedBy":null,
"dateCreated":"2018-05-16T19:29:11.113",
"dateLastUpdated":"2018-05-16T19:29:11.113"
},
{
...
},
...
]
我从我的 spring 应用程序中公开了一个休息控制器,以使用 pathparam "/questions" 返回所有问题
现在我想为以下情况设计 Rest URL(基本上是返回过滤后的问题集的 URL 和返回问题对象的一部分的 URL)。例如:
- 只返回所有问题的标题。
- 仅返回所有技术问题的标题。
- 以算法的技巧返回问题。
我认为没有标准的惯例。有没有?但是,我想听听人们如何为上述用例设计 REST API。我也很想听听这种做法背后的原因。
感谢这里的领导。
【问题讨论】:
-
我投票结束这个问题,因为在为您的 API 设计 URI 时没有对错。答案将仅反映有意见的建议。此外,在 REST 生态系统中,URI 的结构方式甚至都无关紧要,因为客户端将使用有意义的关系名称(如
next、prev、self、first或last)来推断无论如何,URI。像部分资源表示这样的东西应该通过内容协商或部分 GET 请求进行通信(尽管客户端事先已经有一些知识;~ ANTI-REST)
标签: java rest jax-rs spring-restcontroller