具有两个独立主键的实体的 RESTful API 行为 [英] RESTful API behavior for entitys with two independent primary keys
问题描述
我有以下实体:
<car>
<carID>7</carID>
<...>...</...>
<externalCarID>23890212</externalCarID>
</car>
现在的问题是 carID
和 externalCarID
都是由/用于不同系统的独立主键,API 客户端应该可以访问 carID
和 externalCarID
code>car 实体同时具有 carID
x 或 externalCarID
.
The problem is now that carID
and externalCarID
are both independent primary keys that are used by/for different systems and it should be possible for API clients to access a car
entity with both the carID
xor the externalCarID
.
两个键都是整数,并且它们不使用不相交的集合:
Both keys are ints and they do not use disjoint sets:
carID(7) != externalCarID(7)
我想到了以下解决方案:
- 使用
/restapi/car/7
和/restapi/externalcar/23890212
访问它 - 使用参数,例如像
/restapi/car/7?type=regular
和/restapi/car/23890212?type=ext
- 在标题中将信息某处发送 - 但在哪里?
- Access it with
/restapi/car/7
and/restapi/externalcar/23890212
- Use parameters e.g. like
/restapi/car/7?type=regular
and/restapi/car/23890212?type=ext
- Send the information somewhere in the header - but where?
一些关于如何处理此问题的提示或对我的解决方案的反馈,最好参考 REST/HTTP 规范,会很棒!
Some tips how to handle this or feedback on my solutions, preferably with the reference to REST / HTTP specs, would be great!
关于主键的背景:
假设我们的发票系统需要 carID
,而我们的母公司控制系统需要 externalCarID
.我根本不喜欢那样,但它是一个正在运行的系统,我现在无法更改它.
Lets say our invoice systems needs the carID
and our parent company controlling system needs the externalCarID
. I do not like that at all, but its a running system and there is no way for me to change it right now.
推荐答案
我建议您选择一个 ID 作为真正的主要",并按照建议访问它:
I'd recommend that you pick one ID as the 'real primary', and access it as suggested:
/restapi/car/7
如果您控制其中一个 ID,那么我建议将其用作真正的主要".
If you control one of the IDs, then I'd suggest using that as the 'real primary'.
另一个 ID,即使它是唯一标识符,也可能应该使用查询参数访问:
The other ID, even though it's a unique identifier should probably be accessed using a query parameter:
/restapi/car?extid=23384
有一些类似的问题可能会有所帮助:
There are some similar questions which may help:
REST API 设计 - 获取通过 REST 使用不同参数但相同 url 模式的资源
最后要考虑的一件事是将您的extid"网址重定向到该资源的规范网址.
One final thing to consider is redirecting your url for 'extid' to the canonical URL for that resource.
这篇关于具有两个独立主键的实体的 RESTful API 行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!