REST API-更新资源的单个字段 [英] Rest api - update single field of resource

查看:693
本文介绍了REST API-更新资源的单个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我的Driver资源有其他端点. 我有这样的PUT方法

Lets say I have rest endpoint for my Driver resource. I have PUT method like this

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

还是最好使用现有端点?使用现有端点的一个问题是,驱动程序具有很多字段(将近30个),而仅发送所有这些字段仅用于更新已启用"或禁用"驱动程序就太过分了.

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.

您怎么看?

推荐答案

这正是HTTP方法PATCH的用途.在资源有很多字段但只想更新几个字段的情况下使用它.

This is exactly what the HTTP method PATCH is made for. It is used in cases where the resource has many fields but you only want to update a few.

就像PUT一样,您向myapi/drivers/{id}发送请求.但是,与PUT不同,您仅在请求正文中发送要更改的字段.

Just like with PUT, you send a request to myapi/drivers/{id}. However, unlike with PUT, you only send the fields you want to change in the request body.

创建像myapi/drivers/{id}/enable这样的终结点不是非常RESTful,因为启用"本身并不能真正称为资源.

Creating endpoints like myapi/drivers/{id}/enable is not very RESTful, as "enable" can't really be called a resource on its own.

有关Spring PATCH端点的示例实现,请参见此链接.

For an example implementation of a Spring PATCH endpoint, please see this link.

这篇关于REST API-更新资源的单个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆