给定以下类(注意:这些类不在讨论中,它们只是以这种方式存在)
class Foo {}
class Bar {}
class Event {
Foo foo;
Bar bar;
String event;
public Event(Foo foo, String event){
..
}
public Event(Bar bar, String event){
..
}
}
事件与 Foo 或 Bar 相关联,但从不与两者相关。
你会像你的 REST api 那样建模吗(这对你的 api 的用户来说或多或少感觉很自然):
POST /foo/{FOO-ID}/event
GET /foo/{FOO-ID}/event --> gets the list of events for FOO with the given id
GET /foo/{ID}/event/{EVENT-ID}
POST /bar/{BAR-ID}/event
GET /bar/{BAR-ID}/event --> gets the list of events for BAR with the given id
GET /bar/{BAR-ID}/event/{EVENT-ID}
还是您更喜欢(或多或少反映了域模型):
POST /event
GET /event?id=123&type=FOO --> gets the list events of for FOO with id = 123
GET /event?id=456&type=BAR --> gets the list of events for BAR with id = 456
GET /event/{EVENT-ID}
GET /event --> not implemented, it would logically return ALL events(both FOO and BAR), but this has no business meaning
2 个 api 中的哪一个是“最”REST-ful?为什么?