简体   繁体   English

路由 api/v1 中额外的 /v1 的目的是什么

[英]what is the purpose of the extra /v1 in the route api/v1

Why not just make your backend api route start with /api ?为什么不让您的后端 api 路由以/api开头?

Why do we want to have the /v1 bit?为什么我们想要/v1位? Why not just api/ ?为什么不只是api/ Can you give a concrete example?你能举个具体的例子吗? What are the benefits of either?两者都有什么好处?

One of the major challenges surrounding exposing services is handling updates to the API contract.公开服务的主要挑战之一是处理 API 合约的更新。 Clients may not want to update their applications when the API changes, so a versioning strategy becomes crucial.当 API 发生变化时,客户可能不想更新他们的应用程序,因此版本控制策略变得至关重要。 A versioning strategy allows clients to continue using the existing REST API and migrate their applications to the newer API when they are ready.版本控制策略允许客户继续使用现有的 REST API 并在准备好后将其应用程序迁移到较新的 API。

There are four common ways to version a REST API. REST API 有四种常用方法。

  1. Versioning through URI Path http://www.example.com/api/1/products通过 URI 路径http://www.example.com/api/1/products进行版本控制

REST API versioning through the URI path One way to version a REST API is to include the version number in the URI path. REST API 通过 URI 路径进行版本控制 一种 REST ZDB974238714CA8DE634A7ACE1 版本的方法是在 URI 路径中包含版本号。

xMatters uses this strategy, and so do DevOps teams at Facebook, Twitter, Airbnb, and many more. xMatters 使用这种策略,Facebook、Twitter、Airbnb 等的 DevOps 团队也是如此。

The internal version of the API uses the 1.2.3 format, so it looks as follows: API内部版本使用1.2.3格式,所以看起来如下:

MAJOR.MINOR.PATCH主要.次要.补丁

Major version: The version used in the URI and denotes breaking changes to the API.主要版本:URI 中使用的版本,表示对 API 的重大更改。 Internally, a new major version implies creating a new API and the version number is used to route to the correct host.在内部,一个新的主要版本意味着创建一个新的 API 并且版本号用于路由到正确的主机。 Minor and Patch versions: These are transparent to the client and used internally for backward-compatible updates.次要版本和补丁版本:这些对客户端是透明的,并在内部用于向后兼容的更新。 They are usually communicated in change logs to inform clients about a new functionality or a bug fix.它们通常在更改日志中传达,以通知客户有关新功能或错误修复的信息。 This solution often uses URI routing to point to a specific version of the API.此解决方案通常使用 URI 路由来指向 API 的特定版本。 Because cache keys (in this situation URIs) are changed by version, clients can easily cache resources.因为缓存键(在这种情况下是 URI)是按版本更改的,所以客户端可以轻松地缓存资源。 When a new version of the REST API is released, it is perceived as a new entry in the cache.当新版本的 REST API 发布时,它被视为缓存中的新条目。

Pros: Clients can cache resources easily Cons: This solution has a pretty big footprint in the code base as introducing breaking changes implies branching the entire API优点:客户端可以轻松缓存资源缺点:此解决方案在代码库中占用相当大的空间,因为引入重大更改意味着分支整个 API

Ref: https://www.xmatters.com/blog/blog-four-rest-api-versioning-strategies/#:~:text=Clients%20may%20not%20want%20to,API%20when%20they%20are%20ready .参考: https://www.xmatters.com/blog/blog-four-rest-api-versioning-strategies/#:~:text=Clients%20may%20not%20want%20to,API%20when%20they%20are% 20准备好了

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

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