简体   繁体   English

Rest api - 更新资源的单个字段

[英]Rest api - update single field of resource

Lets say I have rest endpoint for my Driver resource. 假设我的驱动程序资源有休息端点。 I have PUT method like this 我有像这样的PUT方法

myapi/drivers/{id}

{body of put method}

I need to add functionality which will allow to 'enable' and 'disable' driver 我需要添加允许“启用”和“禁用”驱动程序的功能

Is it good idea to create new endpoint for that like this? 为此创建新端点是个好主意吗?

PUT myapi/drivers/{id}/enable/false

or it is better to use existing endpoint ? 或者最好使用现有的端点? One problem with using existing endpoint is that driver has lot's of fields(almost 30) and sending all those fields just for updating only 'enabled' or 'disable' driver is something overkill. 使用现有端点的一个问题是驱动程序有很多字段(差不多30个),而发送所有这些字段只是为了更新'启用'或'禁用'驱动程序是有点过分。

What do you think? 你怎么看?

This is exactly what the HTTP method PATCH is made for. 这正是HTTP方法PATCH的用途。 It is used in cases where the resource has many fields but you only want to update a few. 它用于资源包含许多字段但您只想更新一些字段的情况。

Just like with PUT , you send a request to myapi/drivers/{id} . 就像PUT ,您向myapi/drivers/{id}发送请求。 However, unlike with PUT , you only send the fields you want to change in the request body. 但是,与PUT不同,您只需在请求正文中发送要更改的字段。

Creating endpoints like myapi/drivers/{id}/enable is not very RESTful, as "enable" can't really be called a resource on its own. 创建像myapi/drivers/{id}/enable这样的端点并不是非常RESTful,因为“enable”本身并不能真正称为资源。

For an example implementation of a Spring PATCH endpoint, please see this link . 有关Spring PATCH端点的示例实现,请参阅此链接

使用PATCH Http metod更新一个字段

PATCH myapi/drivers/{id}/enable

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

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