对在 REST Web 服务请求中使用参数有疑问吗?
Some doubts about the use of parameters into a request toward a REST web service?
我是 Spring 和 REST Web 服务方面的新手,在学习如何使用 Spring MVC 实现 RESTful Web 服务的教程后,我遇到了以下问题。
所以,进入一个控制器class我有这个方法:
@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 和 return 的 HTTP GET 请求,将检索到的列表转换为 JSON 格式(我认为它取决于 content negotiazion:如果调用者将 Accept header 作为 JSON 方法 return 结果以 JSON 格式,对吗?)
顺便说一下,我的疑问与教程中显示的 HTTP 请求有关,事实上它确实存在:
http://localhost:8080/springchocolatestore/api/categories?start=0&size=2
由先前的控制器方法处理为 return 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 原则。是真的还是我漏了什么?
或者在这种特定情况下可能是有效的,因为参数没有指定 object(必须 returned 到我的 JSON 输出中)但仅与一些选择?
我的意思是,也许我不能使用参数来指定特定的 object,像这样:
// RETRIEVE THE PRODUCT WITH ID=1
http://localhost:8080/springchocolatestore/api/producs?product=1
所以我认为前一个没有遵循 RESTfull 标准,因为我指定了一个带有参数的产品 object 而不是作为资源访问它,所以我必须这样做这样:
http://localhost:8080/springchocolatestore/api/producs/1
你能给我解释一下吗?
Tnx
REST 与 URL 没有太多关系,使用请求参数也不是 RESTful。
但我同意路径变量一般用于标识特定资源,参数一般用于搜索或分页参数
我是 Spring 和 REST Web 服务方面的新手,在学习如何使用 Spring MVC 实现 RESTful Web 服务的教程后,我遇到了以下问题。
所以,进入一个控制器class我有这个方法:
@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 和 return 的 HTTP GET 请求,将检索到的列表转换为 JSON 格式(我认为它取决于 content negotiazion:如果调用者将 Accept header 作为 JSON 方法 return 结果以 JSON 格式,对吗?)
顺便说一下,我的疑问与教程中显示的 HTTP 请求有关,事实上它确实存在:
http://localhost:8080/springchocolatestore/api/categories?start=0&size=2
由先前的控制器方法处理为 return 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 原则。是真的还是我漏了什么?
或者在这种特定情况下可能是有效的,因为参数没有指定 object(必须 returned 到我的 JSON 输出中)但仅与一些选择?
我的意思是,也许我不能使用参数来指定特定的 object,像这样:
// RETRIEVE THE PRODUCT WITH ID=1
http://localhost:8080/springchocolatestore/api/producs?product=1
所以我认为前一个没有遵循 RESTfull 标准,因为我指定了一个带有参数的产品 object 而不是作为资源访问它,所以我必须这样做这样:
http://localhost:8080/springchocolatestore/api/producs/1
你能给我解释一下吗?
Tnx
REST 与 URL 没有太多关系,使用请求参数也不是 RESTful。
但我同意路径变量一般用于标识特定资源,参数一般用于搜索或分页参数