REST-API-为一个资源设计多个端点
REST-API-Design with multiple Endpoints for one Resource
我目前正在设计 REST-API。
假设我想要GET/POST/PUT/DELETE以下对象
假设我有以下我想要的信息GET/PUT/POST/DELETE(这只是一个小例子。真实的对象有更多的属性)
public class Person
{
public int Id{get;set;}
public string Firstname{get;set;}
public string Lastname{get;set;}
public DateTime Birthday{get;set;}
public Gender Gender{get;set;}
public string Title{get;set;}
public Address Address{get;set;}
public Contact[] Contacts{get;set;}
public History[] HistoryItems{get;set;}
public Relative[] Relatives{get;set;}
}
public class Address
{
public string Street{get;set;}
public string ZipCode{get;set;}
public string City{get;set;}
public string State{get;set;}
public string Country{get;set;}
}
public class Contact
{
public string Name{get;set;}
public string Value{get;set;}
}
public class History
{
public DateTime TimeStamp{get;set;}
public string OldValue{get;set;}
public string NewValue{get;set;}
public string ChangedItemd{get;set;}
}
public class Relative
{
public int RelativeType{get;set;}
public Person Person{get;set;}
}
为了提高 API 和客户端的性能(信息显示在 TabControl 内的几个选项卡上。因此应该只加载当前显示的信息)我考虑引入多个 REST-端点。
所以我会
http://.../api/contacts/{personId}
为联系人
http://.../api/addresses/{personId}
为一个人的地址
http://.../api/persons/{personId}
用于此人的 "main"-信息
http://.../api/relatives/{personId}
对于一个人的亲戚
这对 GET 请求来说效果很好。但是我认为 PUT/POST/DELTE-Requests 会有问题。因为一切都应该在事务中持久化到数据库中。这(据我所知)对于多个 REST 请求是不可能的。
那么还有什么选择呢?一个 "big"- 始终从数据库读取所有信息的端点?
我认为你的结构是错误的,联系人(例如)之后的标识符不能是personId
。这应该是之前资源的id。
例如,这条路线:
http://.../api/contacts/{personId}
应该是这样的:
http://.../api/persons/{personId}/contacts
如果您遵循此规则,您的 API 将如下所示:
GET 仅在
http://.../api/persons/{personId}/contacts
http://.../api/persons/{personId}/adresses
http://.../api/persons/{personId}/relatives
GET/POST/PUT/DELETE 在
http://.../api/persons/{personId}
我目前正在设计 REST-API。
假设我想要GET/POST/PUT/DELETE以下对象
假设我有以下我想要的信息GET/PUT/POST/DELETE(这只是一个小例子。真实的对象有更多的属性)
public class Person
{
public int Id{get;set;}
public string Firstname{get;set;}
public string Lastname{get;set;}
public DateTime Birthday{get;set;}
public Gender Gender{get;set;}
public string Title{get;set;}
public Address Address{get;set;}
public Contact[] Contacts{get;set;}
public History[] HistoryItems{get;set;}
public Relative[] Relatives{get;set;}
}
public class Address
{
public string Street{get;set;}
public string ZipCode{get;set;}
public string City{get;set;}
public string State{get;set;}
public string Country{get;set;}
}
public class Contact
{
public string Name{get;set;}
public string Value{get;set;}
}
public class History
{
public DateTime TimeStamp{get;set;}
public string OldValue{get;set;}
public string NewValue{get;set;}
public string ChangedItemd{get;set;}
}
public class Relative
{
public int RelativeType{get;set;}
public Person Person{get;set;}
}
为了提高 API 和客户端的性能(信息显示在 TabControl 内的几个选项卡上。因此应该只加载当前显示的信息)我考虑引入多个 REST-端点。
所以我会
http://.../api/contacts/{personId}
为联系人
http://.../api/addresses/{personId}
为一个人的地址
http://.../api/persons/{personId}
用于此人的 "main"-信息
http://.../api/relatives/{personId}
对于一个人的亲戚
这对 GET 请求来说效果很好。但是我认为 PUT/POST/DELTE-Requests 会有问题。因为一切都应该在事务中持久化到数据库中。这(据我所知)对于多个 REST 请求是不可能的。
那么还有什么选择呢?一个 "big"- 始终从数据库读取所有信息的端点?
我认为你的结构是错误的,联系人(例如)之后的标识符不能是personId
。这应该是之前资源的id。
例如,这条路线:
http://.../api/contacts/{personId}
应该是这样的:
http://.../api/persons/{personId}/contacts
如果您遵循此规则,您的 API 将如下所示:
GET 仅在
http://.../api/persons/{personId}/contacts
http://.../api/persons/{personId}/adresses
http://.../api/persons/{personId}/relatives
GET/POST/PUT/DELETE 在
http://.../api/persons/{personId}