简体   繁体   English

REST-发布JSON列表,或发布多个帖子?

[英]REST - Post a JSON list, or multiple Posts?

Given two models: a Person , which has any number of Jobs , what is the most REST-ful way of submitting a new Person with his list of jobs? 鉴于两个模型:一个Person ,里面有任意数量的Jobs ,什么是提交一个新的人与他的职位列表的最REST-FUL方式?

The two obvious options that come to me are to JSON-ify the Person object with his list of Jobs. 我想到的两个显而易见的选择是使用其Jobs列表对Person对象进行JSON修饰。

"{"name":"ngmiceli",
  "jobs":[
      {"info":"foobar"},
      {"info":"lorem ipsum"},
      {"info":"ni!"}]}"

Stick that in the body, send the Post request, and be on your way. 将其贴在身上,发送“发帖”请求,然后随身携带。

The second obvious model would be much more broken up. 第二种明显的模型将被分解得多。

  • Post a new Person with "name"="ngmiceli" 发表一个新的人与"name"="ngmiceli"
  • Post a new Job to that person. 向该人发布新工作。 (Repeat 3x) (重复3次)

There will be an API controller for both of these actions separately, but if the purpose of this application is on jobs, and a person is fairly meaningless (or even invalid) without jobs, wouldn't it make sense to submit the model of the peron with his/her jobs? 这两个动作将分别有一个API控制器,但是如果此应用程序的目的是在工作上,并且一个人没有工作就毫无意义(甚至无效),那么提交该模型就没有意义了。庇隆他/她的工作?

Finally, I read about batch updates, where you send a list of Post requests that make up the second option, but in a single batched Post. 最后,我阅读了有关批处理更新的信息,在该批处理更新中,您在一个批处理的Post中发送了构成第二个选项的Post请求列表。

If it matters, the requests are being generated by javascript and handled in C# web-api, and are communicating with a database via entity framework. 如果很重要,请求将由javascript生成并在C#web-api中处理,并通过实体框架与数据库进行通信。

There is nothing un-RESTful about number 1. Even if the application is not Person-centered, a Person is a valid resource in your application. 关于数字1并没有令人不安的地方。即使应用程序不是以人员为中心的,人员也是您应用程序中的有效资源。

Go for the simpler solution. 寻找更简单的解决方案。

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

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