简体   繁体   English

我应该如何设计 REST API?

[英]How should I design a REST API?

I am making a REST API with spring framework.我正在使用 spring 框架制作一个 REST API。 Then, I was worried, so I started posting.然后,我很担心,所以我开始发帖。

When there is data as below,当有如下数据时,

GET /users/1获取/用户/1

{
   "name": "mike",
   "age": 21
}

Currently, REST API has been developed as follows.目前,REST API 已经开发如下。

PUT /users/1放置/用户/1

{
   "name": "mike"
}

When requested by PUT as above, the name is changed and the missing attribute age becomes null as follows.当 PUT 如上所述请求时,名称被更改并且缺少的属性 age 变为空值,如下所示。

GET /users/1获取/用户/1

{
   "name": "mike2",
   "age": null
}

PATCH /users/1补丁/用户/1

{
   "name": "mike2"
}

When requesting PATCH as above, the name is changed and the missing attribute age is not changed as follows.像上面这样请求PATCH的时候,改了名字,不改缺失的属性age如下。

GET /users/1获取/用户/1

{
   "name": "mike2",
   "age": 21
}

I want to know if this fits the REST API rules.我想知道这是否符合 REST API 规则。

If this isn't appropriate, how do I request if I want to modify name and delete age in one request?如果这不合适,如果我想在一个请求中修改姓名和删除年龄,我该如何请求?

I need help.我需要帮助。

Reading the RFCs is highly recommended强烈建议阅读 RFC

RFC 7231 Hypertext Transfer Protocol (HTTP/1.1): Semantics and Content https://tools.ietf.org/html/rfc7231 RFC 7231 超文本传输​​协议 (HTTP/1.1):语义和内容https://tools.ietf.org/html/rfc7231

In particular, pay close attention to Section 4.3 Method Definitions特别要注意第 4.3 节方法定义

https://tools.ietf.org/html/rfc7231#section-4.3 https://tools.ietf.org/html/rfc7231#section-4.3

"The POST method requests that the target resource process the representation enclosed in the request according to the resource's own specific semantics." “POST 方法请求目标资源根据资源自己的特定语义处理请求中包含的表示。”

"The PUT method requests that the state of the target resource be created or replaced with the state defined by the representation enclosed in the request message payload." “PUT 方法请求目标资源的状态被创建或替换为由包含在请求消息有效负载中的表示定义的状态。”

Which is to say - that you will find variation in how an update action is processed (some folks use POST, some use PATCH, many use PUT), depending on their implementation-specific resource semantics.也就是说 - 您会发现更新操作的处理方式有所不同(有些人使用 POST,有些人使用 PATCH,许多人使用 PUT),这取决于他们特定于实现的资源语义。

In your example, it appears that the PUT resource-specific semantic implementation is treating the submitted data element as a full replacement - and since you did not include the age element - it is left as null.在您的示例中,似乎 PUT 特定于资源的语义实现将提交的数据元素视为完全替换 - 由于您没有包含年龄元素 - 它被保留为空。

PATCH is indeed intended as the verb for doing a partial update, see: PATCH 确实用作进行部分更新的动词,请参阅:

RFC 5789 PATCH Method for HTTP用于 HTTP 的 RFC 5789 PATCH 方法

https://tools.ietf.org/html/rfc5789 https://tools.ietf.org/html/rfc5789

"The PATCH method requests that a set of changes described in the request entity be applied to the resource identified by the Request-URI. The set of changes is represented in a format called a "patch document" identified by a media type." “PATCH 方法请求将请求实体中描述的一组更改应用于由 Request-URI 标识的资源。这组更改以一种称为“补丁文档”的格式表示,该格式由媒体类型标识。”

"The difference between the PUT and PATCH requests is reflected in the way the server processes the enclosed entity to modify the resource identified by the Request-URI. In a PUT request, the enclosed entity is considered to be a modified version of the resource stored on the origin server, and the client is requesting that the stored version be replaced. With PATCH, however, the enclosed entity contains a set of instructions describing how a resource currently residing on the origin server should be modified to produce a new version. The PATCH method affects the resource identified by the Request-URI, and it also MAY have side effects on other resources; ie, new resources may be created, or existing ones modified, by the application of a PATCH." “PUT和PATCH请求的区别体现在服务器处理封闭实体的方式来修改Request-URI标识的资源。在PUT请求中,封闭实体被认为是所存储资源的修改版本在源服务器上,并且客户端请求替换存储的版本。但是,对于 PATCH,封闭的实体包含一组说明,描述应如何修改当前驻留在源服务器上的资源以生成新版本。 PATCH 方法会影响由 Request-URI 标识的资源,也可能对其他资源产生副作用;即,应用 PATCH 可能会创建新资源或修改现有资源。”

"PATCH is neither safe nor idempotent" “PATCH 既不安全也不幂等”

"There is no guarantee that a resource can be modified with PATCH. Further, it is expected that different patch document formats will be appropriate for different types of resources and that no single format will be appropriate for all types of resources. Therefore, there is no single default patch document format that implementations are required to support." “不能保证可以使用 PATCH 修改资源。此外,预计不同的补丁文档格式将适用于不同类型的资源,并且没有一种格式适用于所有类型的资源。因此,存在没有实现需要支持的单一默认补丁文档格式。”

"Clients need to choose when to use PATCH rather than PUT. For example, if the patch document size is larger than the size of the new resource data that would be used in a PUT, then it might make sense to use PUT instead of PATCH. A comparison to POST is even more difficult, because POST is used in widely varying ways and can encompass PUT and PATCH-like operations if the server chooses. If the operation does not modify the resource identified by the Request-URI in a predictable way, POST should be considered instead of PATCH or PUT." “客户需要选择何时使用 PATCH 而不是 PUT。例如,如果补丁文档大小大于将在 PUT 中使用的新资源数据的大小,那么使用 PUT 而不是 PATCH 可能是有意义的. 与 POST 的比较更加困难,因为 POST 的使用方式多种多样,如果服务器选择,它可以包含类似 PUT 和 PATCH 的操作。如果该操作没有以可预测的方式修改由 Request-URI 标识的资源, 应该考虑 POST 而不是 PATCH 或 PUT。”

For JSON documents, see对于 JSON 文档,请参阅

RFC 6909 - JavaScript Object Notation (JSON) Patch RFC 6909 - JavaScript 对象表示法 (JSON) 补丁

https://tools.ietf.org/html/rfc6902 https://tools.ietf.org/html/rfc6902

"JSON Patch defines a JSON document structure for expressing a sequence of operations to apply to a JavaScript Object Notation (JSON) document; it is suitable for use with the HTTP PATCH method. The "application/json-patch+json" media type is used to identify such patch documents." “JSON Patch 定义了一个 JSON 文档结构,用于表达应用于 JavaScript Object Notation (JSON) 文档的一系列操作;它适合与 HTTP PATCH 方法一起使用。“application/json-patch+json”媒体类型是用于识别此类补丁文件。”

Additionally, you may find these resources useful此外,您可能会发现这些资源很有用

https://restfulapi.net/http-methods/ https://restfulapi.net/http-methods/

https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods

https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PATCH https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PATCH

https://en.wikipedia.org/wiki/Patch_verb https://en.wikipedia.org/wiki/Patch_verb

http://jsonpatch.com/ http://jsonpatch.com/

https://microservices.io/book https://microservices.io/book

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

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