小编Ema*_*uel的帖子

将一种资源附加到另一种资源的 RESTful 方式是什么?

这是我在这个地方找不到相同问题的少数时刻之一,所以我试图描述我的问题并希望得到一些帮助和想法!

比方说...

我想为域模型设计一个 RESTful API,它可能具有如下所示的实体/资源:

class Product
{
    String id;
    String name;
    Price price;
    Set<Tag> tags;
}


class Price
{
    String id;
    String currency;
    float amount;
}


class Tag
{
    String id;
    String name;
}
Run Code Online (Sandbox Code Playgroud)

API 可能如下所示:

GET /products
GET /products/<product-id>
PUT /prices/<price-id>?currency=EUR&amount=12.34
PATCH /products/<product-id>?name=updateOnlyName
Run Code Online (Sandbox Code Playgroud)

当涉及到更新参考文献时:

PATCH /products/<product-id>?price=<price-id>
PATCH /products/<product-id>?price=
Run Code Online (Sandbox Code Playgroud)

可以将产品的价格参考设置为另一个现有价格,或删除该参考。

但是如何将现有标签的新引用添加到产品中?

如果我想将该引用存储在关系数据库中,我需要一个用于该多对多关系的关系表“products_tags”,这为我们带来了一个明确的解决方案:

POST /product_tags [product: <product-id>, tag: <tag-id>]
Run Code Online (Sandbox Code Playgroud)

但是基于文档的 NoSQL 数据库(如 MongoDB)可以将其存储为每个产品的一对多关系,因此我不需要对必须创建的“新资源”进行建模来保存关系。

POST /products/<product-id>/tags/ [name: ...]
    creates a new Tag (in a Product),

PUT /products/<product-id>/tags/<tag-id>?name=
    creates a new …
Run Code Online (Sandbox Code Playgroud)

rest http nosql

5
推荐指数
1
解决办法
2111
查看次数

标签 统计

http ×1

nosql ×1

rest ×1