Rest api - update single field of resource

后端 未结 2 1779
我寻月下人不归
我寻月下人不归 2021-02-03 20:11

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 functi

相关标签:
2条回答
  • 2021-02-03 20:52

    Use PATCH Http metod to update one field

    PATCH myapi/drivers/{id}/enable
    
    0 讨论(0)
  • 2021-02-03 21:03

    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.

    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.

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

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

    0 讨论(0)
提交回复
热议问题