REST api,具有关系的 POST 实体? [英] REST api, POST entity with relationships?

查看:23
本文介绍了REST api,具有关系的 POST 实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,我无法决定如何处理这个问题..我需要知道是否有任何标准方法可以解决这个问题..或者你们是否对这个问题有任何很好的意见.

Im having an issue where I cant decide how to procced on this matter.. and I need to know if there is any standard way to solve this.. or if you guys have any great input for this matter.

问题是我已经开始构建一个非常基本的 API 用于学习目的API 是一个简单的音乐商店.. 其中商店有一些需要和艺术家的专辑.

The thing is that I have started to build a very basic API for learning purpose The API is a simple Music store.. where the store has some Albums which requires and artist.

所以关系是艺术家 <--- 1 ----- * ---> 专辑并且专辑的存在需要专辑有一位艺术家.但艺术家不需要和专辑.

So the Relationship is Artist <--- 1 ----- * ---> Albums and for an album to exist its required that that the album has an artist. But the artist doesnt require and Album.

现在问题来了...

由于这种关系,如果我想创建一个新专辑..我必须发布专辑数据..和整个Arist-data..不仅仅是艺术家的ID..而是整个艺术家..如果你问我,这似乎不是很有效......因为那有很多不必要的.

Due to this relationship if I want to create a new Album.. I would have to post the album data.. AND the entire Arist-data..not just the ID of th artist.. but the whole Artist.. which doesnt seem very effective if you ask me.. since that a whole lot of unnecessary.

据我所知,有两种方法可以解决这个问题...要么我只是发布整个专辑数据/对象,并允许对象的艺术家数据仅包含一个 ID,然后引用该艺术家.

So as I see it there is two ways to solve this... either I just post the entire album-data/object and allows the Artist-data for the object to only contain an ID which then reference to the artist.

所以,而不是发布:

{
"Name":"AlbumName",
"Description":"Some description for the album",
"ReleaseYear": "1992",
"Artist" {
    "Name":"Some artist",
    "Id":"12345",
    "Biography":"Lorem ipsum dolor sit amet"
    }
}

我会这样做:

{
    "Name":"AlbumName",
    "Description":"Some description for the album",
    "ReleaseYear": "1992",
    "Artist" {
        "Id":"12345"
        }
    }

第二个选项是实际上有一个特定于此的路由/url...例如:/api/artist/{artistid}/专辑然后只需将专辑对象发布到该网址..

Option number two is to actually have a route/url thats specific for this... for instance: /api/artist/{artistid}/album And then simply post an album-object to that url..

但正如我所说..我真的不确定这里的对与错..或者有任何标准的处理方法吗?

But as I said.. Im really not sure of whats right and wrong here.. or is there any standard way of handling this?

提前致谢!

推荐答案

我会建议这样的事情.

POST /musicstore/artist/343/albums

{
    "Name":"AlbumName",
    "Description":"Some description for the album",
    "ReleaseYear": "1992",
}

创建资源作为艺术家 343 专辑集合的子资源的行为隐含地创建了艺术家和专辑之间的关系.无需在有效载荷中指定.

The act of creating a resource as a child of the collection of albums for the artist 343 implicitly creates the relationship between the artist and the album. There is no need to specify it in the payload.

这篇关于REST api,具有关系的 POST 实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆