RESTful API URI 设计 [英] RESTful API URI Design

查看:44
本文介绍了RESTful API URI 设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找有关 RESTful API 的 URI 设计的方向.我将有几个嵌套的链接资源,目前我设计的 URI 类似于这篇文章:Hierarchical RESTful URL设计

I'm looking for some direction in regards to the URI design for a RESTful API. I'm going to have several nested linked resources and have currently designed my URI's similar to this post: Hierarchical RESTful URL design

以下示例不是我正在构建的示例,但我认为很好地说明了我的情况.(假设一个节目只能属于一个网络).

The following example isn't what I'm building but I think illustrates my situation well. (Assume that a show can only belong to one network).

/networks [GET,POST]
/networks/{network_id} [GET,PUT]
/networks/{network_id}/shows [GET,POST]
/networks/{network_id}/shows/{show_id} [GET,PUT]
/networks/{network_id}/shows/{show_id}/episodes [GET,POST]
/networks/{network_id}/shows/{show_id}/episodes/{episode_id} [GET,PUT]

我的情况将通过关联进一步提升两个级别,但所有关联都是一对多的.我正在考虑将其切换为类似于此的内容:

My situation will go two more levels further with associations but all the associations are one to many. I'm considering switching it to something similar to this:

/networks [GET,POST]
/networks/{network_id} [GET,PUT]
/networks/{network_id}/shows [GET,POST]

/shows [GET]
/shows/{id} [GET,PUT]
/shows/{id}/episodes [GET,POST]

/episodes [GET]
/episodes/{id} [GET,PUT]

我的问题是:

  1. 第二个示例是有效的 REST 设计吗?
  2. 我是否应该考虑同时实施两条路径?

推荐答案

我觉得第二个例子很好.这些 URL 描述了资源并且使用了正确的 HTTP 动词.

The second example looks fine to me. The URLs are descriptive of the resources and the correct HTTP verbs are being used.

如果有道理,多个 URL 指向同一个资源是完全没问题的.但更重要的是,确保资源包含将节目连接到网络、剧集到节目等的 元素.

It is perfectly fine to have multiple URLs pointing to the same resource, if that makes sense. But more importantly, make sure the resources contain <link /> elements that connect shows to networks, episodes to shows, etc.

这篇关于RESTful API URI 设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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