简体   繁体   English

REST API子资源

[英]REST API sub resources

Creating my first REST API and I have a question. 创建我的第一个REST API,我有一个问题。 I have a resource venue: 我有一个资源场所:

/venues

each venue has guests: 每个场地都有客人:

/venues/1/guests

This is where I am confused about. 这是我感到困惑的地方。 If I want to update a guest resource, is it better to do this: 如果我想更新来宾资源,最好这样做:

POST /venue/1/guests/1/

or 要么

POST /guests/1

The second option is shorter and since guests ids are unique, the 2nd one works just fine. 第二个选项更短,因为客人ID是唯一的,第二个选项工作得很好。 But first is more explicit. 但首先是更明确的。

So I am stuck on which route to take from here. 所以我被困在哪条路上。

Thanks 谢谢

Both are pretty much the right approaches although, if you guarantee the clients (client developers) that the guest ids are always going to be unique regardless of the venue they visit, I will go with 两者都是正确的方法,但是,如果你保证客户(客户开发人员)客人ID总是独特的,无论他们访问的场地如何,我都会去

POST /guests/{guestId}

I will normally use this: 我通常会用这个:

POST /venues/{venueId}/guests/{guestId}

when I have to change the status of that guest for that given venue. 当我必须改变那个特定场地的客人的状态时。 For example, Guest John Doe with id 1 has RSVP'ed "yes" for the venue with venueId 1 then I will POST the data to the following url (this is just an example where it is assumed that the only thing you can add/update for a given guest for a given venue is the RSVP data): 例如,ID为1的Guest John Doe对于具有venueId 1的场所已经RSVP'为“yes”然后我将数据发布到以下url(这只是假设您可以添加的唯一内容/对于给定地点的给定客人的更新是RSVP数据):

POST /venues/1/guests/1
request body: {"RSVP", true}

but if I want to update John Doe's name to John Foo, I will probably do 但如果我想将John Doe的名字更新为John Foo,我可能会这样做

PUT /guests/1
request body: {"firstName":"John","lastName":"Foo"}

I hope that helps! 我希望有所帮助!

You can think of you having two RESTful resources to manage 您可以认为有两个RESTful资源需要管理

  • A) Venues and A)场地和
  • B) Guests B)客人

These resources can be independently managed using POST/GET/DELETE/PUT (CRUD operations) 可以使用POST / GET / DELETE / PUT(CRUD操作)独立管理这些资源

POST /venues/
GET /venues/
POST /guests/
GET /guests/

Also you can use CRUD to map one resource to another like 您也可以使用CRUD将一个资源映射到另一个资源

POST /venues/[venue-id]/guests/[guest-id]/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM