REST API 中集合与项目的部分表示

编程入门 行业动态 更新时间:2024-10-09 11:27:52
本文介绍了REST API 中集合与项目的部分表示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在组合一个基于 REST 的 API,但我不确定我应该如何为集合和单个资源提供响应.

I'm putting together a REST based API but I'm not sure on how I should deliver the response for collections vs individual resources.

在 REST 世界中对单个项目的集合进行精简表示有意义吗?

Does it make sense to have a slimmed down representation for a collection over a single item in the world of REST?

假设我有一些类似的专辑收藏:

Say I have something along the lines of this for a collection of albums:

{
    items: [
        {
            "id": 1,
            "title": "Thriller"
        },
        ...
    ]
}

但是对于我拥有的实际单个项目

But then for the actual individual item I had

{
    "id": 1,
    "title": "Thriller",
    "artist": "Michael Jackson",
    "released": "1982",
    "imageLinks": {
        "smallThumbnail": "...",
        "largeThumbnail": "..."
    }
    ...
}

推荐答案

一个资源表示应该是唯一的,无论它是作为一个集合还是单个项目给出.但是,您可以引入一个像 fields 这样的新参数,客户端可以使用它来仅获取所需的字段,从而优化带宽.

A resource representation should be unique irrespective of whether it is given as a collection or a single item. But, you can introduce a new parameter like fields which can be used by the clients to get only the required field thereby optimising the bandwidth.

/albums - 这应该给出对象列表,每个对象都具有您在单个项目 api 中提供的结构

/albums - This should give the list of objects each having the structure of what you would give in a individual item api

/albums?fields=id,title - 这可以给出只有 id & 的对象列表.标题.

/albums?fields=id,title - This can give the list of objects with just the id & title.

这篇关于REST API 中集合与项目的部分表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-18 03:49:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/928287.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:项目   REST   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!