简体   繁体   English

@PatchMapping 的用例

[英]Use cases of @PatchMapping

There is a RequestMethod named PATCH .有一个名为PATCHRequestMethod

To use this method, we can define @PatchMapping for a rest endpoint.要使用此方法,我们可以为 rest 端点定义@PatchMapping As per my understanding, it sounds like partially updating the DB object.据我了解,这听起来像是部分更新了 DB object。

Generally, we use POST or PUT calls to perform save or update.通常,我们使用 POST 或 PUT 调用来执行保存或更新。 So, still not clear what are exact use cases of PatchMapping and why can't I just use PUT instead of PATCH?所以,仍然不清楚 PatchMapping 的确切用例是什么,为什么我不能只使用 PUT 而不是 PATCH?

still not clear what are exact use cases of PatchMapping and why can't I just use PUT instead of PATCH?仍然不清楚 PatchMapping 的确切用例是什么,为什么我不能只使用 PUT 而不是 PATCH?

PUT (defined by RFC 7231 ) and PATCH (defined by RFC 5789 ) are two different methods used for a similar purpose: to request that the server make its representation of a resource match the representation on the client. PUT(由RFC 7231定义)和 PATCH(由RFC 5789定义)是用于类似目的的两种不同方法:请求服务器使其资源表示与客户端上的表示匹配。

Imagine, if you would, trying to update a web page provided by a server.想象一下,如果您愿意,尝试更新服务器提供的 web 页面。 The client first obtains a recent copy of the server's representation:客户端首先获取服务器表示的最新副本:

GET /foo

and then, using the client's favorite local HTML editor, makes changes to this private copy.然后,使用客户最喜欢的本地 HTML 编辑器对此私有副本进行更改。 When the client has finished making the changes, we want to send those changes back to the server to be used.当客户端完成更改后,我们希望将这些更改发送回服务器以供使用。

The straight forward way to do this in HTTP is to simply send the entire updated representation back to the server:在 HTTP 中执行此操作的直接方法是将整个更新的表示简单地发送回服务器:

PUT /foo

<html>....</html>

When the representation is very large (compared with the HTTP headers), and the edits are very small (compared to the document), then PUT becomes a somewhat "expensive" way to achieve what ought to be a small thing.当表示非常大(与 HTTP 标头相比)并且编辑非常小(与文档相比)时,那么 PUT 就变成了一种有点“昂贵”的方式来实现应该是一件小事。

To that end, we might also support PATCH, so that instead of sending the entire document, we just send a representation of the changes we made: a patch document.为此,我们可能还支持 PATCH,这样我们就不需要发送整个文档,而是发送我们所做更改的表示:一个补丁文档。

When the server receives our patch, it loads its own copy of the document, applies the changes described by the patch document, and saves the result.当服务器收到我们的补丁时,它会加载它自己的文档副本,应用补丁文档描述的更改,并保存结果。

Thus: the overall use case is the same: remote authoring.因此:总体用例是相同的:远程创作。 You load a representation of a resource into your HTTP aware document editor, make a few changes, and hit "save", and your editor knows what to do to communicate your edits back to the server.您将资源的表示加载到 HTTP 感知文档编辑器中,进行一些更改,然后点击“保存”,您的编辑器知道如何将您的编辑传达回服务器。

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

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