我在Spring和REST服务方面非常新,下面的教程介绍了如何使用Spring实现RESTful web服务。
因此,在控制器类中,我有这样的方法:
@Controller
@RequestMapping("/api/categories")
public class CategoryRestController {
@RequestMapping
@ResponseBody
public CategoryList getCategories(@RequestParam("start") int start, @RequestParam("size") int size ) {
List<Category> categoryEntries = categoryService.findCategoryEntries(start, size);
return new CategoryList(categoryEntries);
}
}该方法处理针对间接性/api/categories的HTTP请求,并将检索到的列表返回为JSON格式(我认为这取决于内容协商:如果调用方将接受标头作为JSON放入JSON,则方法以JSON格式返回结果,对吗?)
顺便说一句,我的怀疑与教程中显示的HTTP请求有关,实际上它是这样做的:
http://localhost:8080/springchocolatestore/api/categories?start=0&size=2它由前面的控制器方法处理,以返回JSON格式的分页列表(可能是hude),实际上,我检索了以下输出:
{
"categories": [
{
"links": [
{
"rel": "self",
"href": "http://localhost:8080/springchocolatestore/api/categories/1",
"variables": [],
"templated": false,
"variableNames": []
}
],
"name": "Truffles",
"description": "Truffles",
"id": {
"rel": "self",
"href": "http://localhost:8080/springchocolatestore/api/categories/1",
"variables": [],
"templated": false,
"variableNames": []
}
},
{
"links": [
{
"rel": "self",
"href": "http://localhost:8080/springchocolatestore/api/categories/2",
"variables": [],
"templated": false,
"variableNames": []
}
],
"name": "Belgian Chocolates",
"description": "Belgian Chocolates",
"id": {
"rel": "self",
"href": "http://localhost:8080/springchocolatestore/api/categories/2",
"variables": [],
"templated": false,
"variableNames": []
}
}
]
}好的,所以在请求中我通过categories?start=0&size=2指定了分页参数
我的怀疑与这个参数的用户有关。据我所理解(但可能是错误的),参数的使用违背了RESTful原则。是真的还是我错过了什么?
或者在这种特定情况下是有效的,因为参数没有指定对象(必须返回到我的JSON输出中),而只与一些选项相关?
我的意思是,我可能不能使用参数来指定特定的对象,如下所示:
// RETRIEVE THE PRODUCT WITH ID=1
http://localhost:8080/springchocolatestore/api/producs?product=1因此,我认为前面的内容没有遵循RESTfull标准,因为我指定了一个带有参数的产品对象,而不是将其作为资源访问,所以我必须这样做:
http://localhost:8080/springchocolatestore/api/producs/1你能给我澄清一下吗?
Tnx
发布于 2015-09-20 10:56:25
REST与URL没有多大关系,使用请求参数也不是unRESTful。
但我同意,路径变量通常用于标识特定的资源,而参数通常用于搜索或分页参数。
https://stackoverflow.com/questions/32678315
复制相似问题