使用扁平化还是不扁平化主体调用HTTP POST API?
我正在开发一个可以返回用户列表的API:使用扁平化还是不扁平化主体调用HTTP POST API?,api,asp.net-core,api-design,Api,Asp.net Core,Api Design,我正在开发一个可以返回用户列表的API: "users": [ { "userId": 1, "name": "John Smith" "address": { "countryCode": "gb", "street": "Orford Street 20", "locality": "London" } "skills": [ { "skillId": 1, "name": "design" },
"users": [
{
"userId": 1,
"name": "John Smith"
"address": {
"countryCode": "gb",
"street": "Orford Street 20",
"locality": "London"
}
"skills": [
{ "skillId": 1, "name": "design" },
{ "skillId": 2, "name": "logotype" }
]
}
]
考虑到我需要创建一个具有地址和技能的用户
哪种正文格式更常用?有点扁平化:
"users": [
{
"name": "John Smith"
"addressCountryCode": "gb",
"addressStreet": "Orford Street 20",
"addressLocality": "London"
"skills": [ "design", "logotype" ]
}
]
可能与GET响应的格式相同:
"users": [
{
"name": "John Smith"
"address": {
"countryCode": "gb",
"street": "Orford Street 20",
"locality": "London"
}
"skills": [
{ "name": "design" },
{ "name": "logotype" }
]
}
]
或者可以分步骤创建用户:
或者甚至可以使用另一种方法?如果您想遵循Rest设计,最好为每个操作都有一个端点,因此第三种方法是您应该使用的方法。 这种方式有一些优点: