REST API,人际关系POST实体? [英] REST api, POST entity with relationships?

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

问题描述

林有一个问题,我不能决定如何procced对此事..和我需要知道是否有解决这个任何标准方式..或者,如果你们对此事的任何大投入。

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..not个艺术家只是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.

因此​​,而不是发帖:

So instead of posting:

{
"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 /艺术家/ {} 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天全站免登陆