Spring 对于在对RESTWeb服务的请求中使用参数有什么疑问?
在Spring和RESTWeb服务方面我是个新手,在一个演示如何使用SpringMVC实现RESTfulWeb服务的教程之后,我有以下几个问题 因此,在控制器类中,我有以下方法:Spring 对于在对RESTWeb服务的请求中使用参数有什么疑问?,spring,web-services,rest,spring-mvc,spring-rest,Spring,Web Services,Rest,Spring Mvc,Spring Rest,在Spring和RESTWeb服务方面我是个新手,在一个演示如何使用SpringMVC实现RESTfulWeb服务的教程之后,我有以下几个问题 因此,在控制器类中,我有以下方法: @Controller @RequestMapping("/api/categories") public class CategoryRestController { @RequestMapping @ResponseBody public CategoryList getCategorie
@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);
}
}
由前面的控制器方法处理以返回分页列表,该列表可以是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": []
}
}
]
}
好的,所以在请求中我按类别指定分页参数?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
你能给我澄清一下吗
TnxREST与url没有太多关系,使用请求参数也不是没有效率的
但我同意路径变量通常用于标识特定资源,参数通常用于搜索或分页参数。你错了。使用参数并没有什么不成功的地方。@JBNizet ohhh我信任你,我经常读你的评论,我知道你对软件体系结构有着深刻的了解。但是,使用参数来指定特定的对象不也是违反RESTful体系结构的吗?RESTful与URL没有多大关系。但我同意路径变量通常用于标识特定资源,参数通常用于搜索或分页参数。好的,您的评论非常清楚…如果您想将其作为响应简单地复制和粘贴,我将接受:-
http://localhost:8080/springchocolatestore/api/producs/1