带有集合的RESTful类的URL?

时间:2013-02-05 17:18:46

标签: web-services rest web-applications

我实施了一个网络应用程序,并希望遵守RESTfull设计标准,我的问题是。

给定一个带有集合的类。

人{     id ::整数     name :: string     朋友们::人物的集合 }

什么是“最”'实现Person-class接口的正确方法:

1

example.com/REST/persons -> [... {id: '2', name: 'Pete', friends: [1,4,6]} ... ]
example.com/REST/persons/2 -> {id: '2', name: 'Pete', friends: [1,4,6]}

通过在人员班上使用一些搜索功能来查找朋友?

2

example.com/REST/persons -> [... {id: '2', name: 'Pete'} ... ]
example.com/REST/persons/2 -> {id: '2', name: 'Pete'}
example.com/REST/persons/2/friends -> [{id: '1', name: 'Joann'}, 
                                       {id: '4', name: 'Jim'},
                                       {id: '6', name: 'Charlie'}]

将集合中的元素作为人员实例中的字段进行查找。

我已经看到这两件事都已实施,但我想遵守标准。

对我来说只是一种兴奋的方式。

example.com/REST/persons/2 -> {id     : '2', 
                               name   : 'Pete', 
                               friends: [example.com/REST/person/1,
                                         example.com/REST/person/4,
                                         example.com/REST/person/6]}

因此使接口事件更加RESTfull - 客户端现在可以遍历整个接口而不了解任何实现。

我希望那里有一些输入。

1 个答案:

答案 0 :(得分:1)

拥有example.com/REST/persons/{person-id}/friends/{friends-id}是一种正确的方法。 REST本身并不强制执行任何URL组合规则,但大多数人都这样做。

基本上,您有一个资源Person和一个具有多对多关系的子资源Friend