如何进行 REST-ful 更新? [英] How to do REST-ful updates?

查看:54
本文介绍了如何进行 REST-ful 更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个对象,比如 Employee,我想提供 2 种不同的更新方式——更新绩效评级,或更新联系信息.

If I have an object, say Employee, and I want to offer 2 different ways of updating it -- update performance rating, or update contact info.

构建 API 的 REST-ful 方式是什么?我认为正确的方法是 POST.

What is the REST-ful way of structuring the API? I assume the right method is POST.

我担心的是,用户首先获取对象的两个部分(性能评级和联系信息),只更新一个部分,然后发布整个更新的对象,这似乎是不雅的.

My concern is that it seems inelegant for a user to first GET both parts of the object (performance rating and contact info), update just one part, and POST the entire updated object.

我的另一个担忧是,发送一个只填充某些字段的对象似乎不雅,因为对象的架构要求所有字段都完整,而省略字段只是为了支持 POST.IE.它需要单独的模式来支持操作或不使用模式——两者似乎都不对.

My other concern is that it seems inelegant to sent an object with only certain fields filled in because the schema for the object requires all fields to be complete, and the omission of fields is only to support the POST. I. e. it would need separate schemas just to support the operations or doing without schemas -- neither of seems right.

同样,使用标志来更新哪些字段也需要不同的模式来进行操作.

Similarly, using flags for what fields to update also requires a different schema just for the operation.

提供单独的方法显然不适合名词-动词范式.

Providing separate methods does not fit in any obvious way into the noun-verb paradigm.

REST API 在需要处理此类情况时应该如何处理?REST 名词和应用程序实体之间是否需要多对一的映射?如果是这样,我们是否限制用于表示应用程序实体子集的视图实体的 PUT?

HOW are REST APIs supposed to look when they are required to handle such cases? Does there need to be a many-to-one mapping between REST nouns and application entities? If so, do we restrict PUT for the view entities which would represent subsets of application entities?

我不是在寻找黑客或不雅的方式.我正在寻找 REST 哲学认为正确的解决方案.提前致谢!

I am not looking for a hack or an inelegant way. I am looking for what the REST philosophy would consider the right solution. Thanks in advance!

推荐答案

我不确定这是否能解决您的顾虑,但看到 Rating 和 Info 也是资源,我会制作这样的 API(感谢 taylonr模板:)

I'm not sure if this fights against your concerns, but seeing how Rating and Info are also resources I would make the API like this (thanks taylonr for the template :)

url/employees/{id}/[rating|info]

通过这种方式,您可以获取所有员工的表示、具有特定 id 的员工、具有特定 id 的员工的绩效评级以及具有特定 id 的员工的联系信息.

This way you can GET a representation of all the employees, an employee with a certain id, performance rating of an employee with a certain id and the contact info of an employee with a certain id.

使用初始化的联系信息和绩效评级创建新员工

Creating a new employee with initialized contact info and performance rating

POST url/employees { "id": "johnsmith", "rating": 4, "info": ["address": "street 1 a 1" , "email": "johns.old@mail.com"] }

更新 2 将适用于不同的 POST

Updating the 2 would work with different POSTs

PUT url/employees/johnsmith/rating  { "rating": 5 }
PUT url/employees/johnsmith/info { "email": "john.smith@company.com" }

这篇关于如何进行 REST-ful 更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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