REST api版本控制(仅对表示形式进行版本控制,而不对资源本身进行版本控制) [英] REST api versioning (only version the representation, not the resource itself)

查看:202
本文介绍了REST api版本控制(仅对表示形式进行版本控制,而不对资源本身进行版本控制)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查看了 API版本控制的最佳做法?,但不是我很相信答案,所以我再次用一个更具体的例子来质疑版本控制部分.我有两个URI(一个带有URI的版本控制,一个没有版本):

I had a look at Best practices for API versioning?, but am not quite convinced of the answer, so I am question the versioning part again with a more specific example. I am having two URIs (one with versioning as part of the URI and one without):

http://xxxx/v1/user/123    -> favored solution in discussed thread
http://xxxx/user/123             

我怀疑第一个链接是否表达了REST的想法.我发现http://xxxx/v1/user/123令人困惑,因为它暗示有朝一日像http://xxxx/v2/user/123这样会有更高的api版本.但这在REST方面没有意义,因为api版本本身是HTTP 1.0或1.1,已在HTTP请求中发送.这种以REST资源为中心的视图与其他api接口(例如SOAP或Java接口)(通常使用合格名称的api版本)非常不同.

I am having my doubts whether the first link expresses the idea of REST. I find http://xxxx/v1/user/123 confusing as it suggests that there will be a higher api-version someday like http://xxxx/v2/user/123. But this does not make sense in REST terms, the api version itself is HTTP 1.0 or 1.1, which is already sent inside the HTTP request. This REST resource centric view differs very from other api-interfaces like SOAP or Java-interfaces (where it is common to have api-versions in qualified names).

在REST中,唯一有意义的版本控制就是该资源的表示形式(例如,添加或删除新字段).此版本控制属于内容协商的一部分,例如:

At REST the only thing where versioning makes sense is the representation of that resource (e.g. new fields are added or removed). This versioning belongs to the part of content-negotiation like:

http://xxx/user/123 + HTTP 'Accept' Header -> Content negotation through header
http://xxx/user/123?v=1                    -> for perma-links/hyperlinks

有人可能会争辩说,这种版本的内容协商可能是路径内URI的一部分,但我发现它是违反直觉的,因为对于相同的资源,您可能最终使用不同的URI,并且必须在一点.

One could also argue that such version content-negotiation could be part of the URI inside the path, but I find it counter-intuitive, because you could end-up with different URIs for the same resource and have to maintain redirects at some point.

总结:在REST URI中,没有api版本控制,只有资源表示形式的版本控制.表示形式版本信息属于内容协商(如queryParam或HTTP'Accept').

To sum-up: In REST URIs there is no api-versioning, only versioning of the resource's representation. Representation version-info belongs to content-negotiation (as queryParam or HTTP 'Accept').

您怎么看?您会在哪些方面不同意/同意?

What do you think? In which things would you disagree/agree?

推荐答案

我完全同意; URI表示身份,引入新版本后身份不会改变.当然,可能会有用于其他概念的新URI,并且现有URI可能会重定向……但是在URI中包含"v2"对我来说是RPCish的.

I completely agree; a URI expresses identity, identity doesn't change when a new version is introduced. There might be new URIs for additional concepts, of course, and existing URIs might redirect … but including a "v2" in the URI smells RPCish to me.

请注意,这实际上与REST无关,因为从REST的角度来看,这全都是字符.

Note that this has got nothing to do with REST, really, as from a REST perspective it's all just characters.

这篇关于REST api版本控制(仅对表示形式进行版本控制,而不对资源本身进行版本控制)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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