简体   繁体   English

版本化RESTful服务?

[英]Versioning RESTful services?

I have a RESTful web service deployed at http://example.com/v1/SomeResource . 我在http://example.com/v1/SomeResource上部署了RESTful Web服务。 One day, a new protocol version ( that is not backwards compatible ) is deployed to http://example.com/v2/SomeResource . 有一天,新的协议版本( 不向后兼容 )被部署到http://example.com/v2/SomeResource From the client of view, this upgrade could occur at any time between two HTTP requests. 从客户端看,此升级可能在两个HTTP请求之间的任何时间发生。

How does the server indicate to the client that it no longer supports v1 calls and the client is expected to upgrade to v2? 服务器如何向客户端指示它不再支持v1调用,并且客户端应该升级到v2? Is there an appropriate response code I can use? 我可以使用适当的响应代码吗?

I would want to provide the client with the following information: 我想向客户提供以下信息:

  1. An incompatible upgrade has taken place. 发生了不兼容的升级。 There is no way for the client to use the new service because the protocol could be totally different. 客户端无法使用新服务,因为协议可能完全不同。
  2. The URL of the new client software. 新客户端软件的URL。
  3. A message explaining to the users that they must upgrade. 向用户解释他们必须升级的消息。

I recommend the following article by Peter Williams 我推荐Peter Williams的以下文章

Best practice: 最佳实践:

It's probably better to keep the versioning out of the URL and to make the new resources backwards compatible with the old. 最好将版本控制保留在URL之外,并使新资源向后兼容旧版本。

Backwards compatible: 向后兼容:

If you must keep the v1 in the URL, and are making v2 URLs, then you have to decide whether you want to support both formats, or make the old v1 obsolete. 如果您必须将v1保留在URL中,并且正在制作v2 URL,那么您必须决定是否要支持这两种格式,或者使旧的v1过时。 If you decide on making the old v1 obsolete then I would recommend to return 303 or 401 for anyone requesting a v1 URL. 如果您决定使旧的v1过时,那么我建议为请求v1 URL的任何人返回303或401。

Making old URLs obsolete: 使旧网址过时:

I would recommend using 303 See Other. 我建议使用303见其他。 Or if there is no associated redirect, then use 410 Gone. 或者,如果没有关联的重定向,则使用410 Gone。

Source 资源

303 See Other 303见其他

The response to the request can be found under a different URI and SHOULD be retrieved using a GET method on that resource. 可以在不同的URI下找到对请求的响应,并且应该使用该资源上的GET方法检索。 This method exists primarily to allow the output of a POST-activated script to redirect the user agent to a selected resource. 此方法主要用于允许输出POST激活的脚本以将用户代理重定向到选定的资源。 The new URI is not a substitute reference for the originally requested resource. 新URI不是最初请求的资源的替代引用。 The 303 response MUST NOT be cached, but the response to the second (redirected) request might be cacheable. 303响应绝不能被缓存,但对第二个(重定向)请求的响应可能是可缓存的。

The different URI SHOULD be given by the Location field in the response. 不同的URI应该由响应中的Location字段给出。 Unless the request method was HEAD, the entity of the response SHOULD contain a short hypertext note with a hyperlink to the new URI(s). 除非请求方法是HEAD,否则响应的实体应该包含一个带有指向新URI的超链接的短超文本注释。

Note: Many pre-HTTP/1.1 user agents do not understand the 303 status. 注意:许多pre-HTTP / 1.1用户代理不了解303状态。 When interoperability with such clients is a concern, the 302 status code may be used instead, since most user agents react to a 302 response as described here for 303. 当考虑与这样的客户端的互操作性时,可以替代地使用302状态代码,因为大多数用户代理对302响应做出反应,如针对303所描述的。

Document everything: 记录一切:

The main thing to be concerned about is whatever you chose to return, just document it in your documentation. 需要关注的主要问题是您选择返回的内容,只需在文档中记录即可。 You can decide how you want your service to work, others that want to consume it will follow the documentation. 您可以决定服务的工作方式,其他想要使用它的人也会遵循文档。

I think you shouldn't do this in the first place, but probably it's too late. 我认为你不应该首先这样做,但可能为时已晚。

URIs should also be static so that when the resource changes or the implementation of the service changes, the link stays the same. URI也应该是静态的,以便在资源更改或服务实现发生更改时,链接保持不变。 This allows bookmarking. 这允许书签。 It's also important that the relationship between resources that's encoded in the URIs remains independent of the way the relationships are represented where they are stored. 在URI中编码的资源之间的关系保持独立于存储它们的关系的方式也很重要。

From the article RESTful Web services: The basics . 从文章RESTful Web服务:基础知识

I would recomend instead the use of the 301 (301 Moved Permanently). 我建议改为使用301(301 Moved Permanently)。 Read why . 了解原因

Hope it helps, Bruno Figueiredo 希望它有所帮助,Bruno Figueiredo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM