接触到RESTful后,在Spring框架的使用过程中我越来越关注HTTP方法,觉得有必要去重新认识所有的HTTP请求方法,特别是PATCH请求,PATCH请求也是为了完成修改资源的任务而存在。下面以菜鸟观点分析下PATCH请求:
- PATCH:
也许你常用的HTTP请求方式就是GET、POST,有的人会用过DELETE、PUT,其实在HTTP1.1中提到过更多的方法。PATCH在早期的HTTP文档 RFC2616中提到过 ,但是却没有完全定义它,直到2010年的RFC5789规范中才真正定义了新的HTTP1.1方法-PATCH。
- 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. If the Request-URI does not point to an existing resource, the server MAY create a new resource, depending on the patch document type (whether it can logically modify a null resource) and permissions, etc.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; i.e., new resources may be created, or existing ones modified, by the application of a PATCH.
这里就不列举出所有PATCH相关的定义了,可以到RFC5789规范中查找。
HTTP中为了提高交互操作性与防止错误,确实需要一种新的修改方法,而PUT方法已经被定义为用一个请求体去修改一个完整的资源。并且不能重复做部分更改,否则代理和缓存、托福学习计划甚至服务器或者客户端都会得到有问题的操作结果。
至此,PATCH方法有了被完全定义的必要。
PATCH在请求中定义了一个描述修改的实体集合,如果被请求修改的资源不存在,服务器可能会创建一个新的资源。
PUT与PATCH的区别反映在服务器内部修改资源的方式中。
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; i.e., new resources
may be created, or existing ones modified, by the application of a
PATCH.
简单来说就是:
PATCH方法用于资源的部分内容的更新;
PUT用于更新某个较为完整的资源;
PATCH方法可能会在资源不存在时去创建它。
PUT | PATCH |
---|---|
幂等 | 非幂等 |
副作用 | 副作用 |
PUT与PATCH都可以带RequestBody请求
- 实现
Spring与JBoss Netty在后面的版本实现了PATCH方法的支持
文章来源:https://blog.csdn.net/No_Endless/article/details/53319576