简体   繁体   English

REST API 设计 - 单个通用端点或多个特定端点

[英]REST API Design - Single General Endpoint or Many Specific endpoints

This is a relatively subjective question, but I want to get other people's opinion nonetheless这是一个比较主观的问题,但我还是想听听别人的意见

I am designing a REST Api that will be accessed by internal systems (a couple of clients apps at most).我正在设计一个可由内部系统(最多几个客户端应用程序)访问的 REST Api。

In general the API needs to update parameters of different car brands.一般来说,API 需要更新不同汽车品牌的参数。 Each car brand has around 20 properties, some of which are shared between all car brands, and some specific for each brand.每个汽车品牌都有大约 20 个属性,其中一些属性在所有汽车品牌之间共享,有些是针对每个品牌的。

I am wondering what is a better approach to the design for the endpoints of this API.我想知道设计此 API 端点的更好方法是什么。

  1. Whether I should use a single endpoint, that takes in a string - that is a JSON of all the properties of the car brand, along with an ID of the car brand.我是否应该使用单个端点,它接受一个字符串 - 这是汽车品牌所有属性的 JSON,以及汽车品牌的 ID。

  2. Or should I provide a separate endpoint per car brand, that has a body with the exact properties necessary for that car brand.或者我应该为每个汽车品牌提供一个单独的端点,该端点具有该汽车品牌所需的确切属性。

So in the first approach I have a single endpoint that has a string parameter that I expect to be a JSON with all necessary values所以在第一种方法中,我有一个端点,它有一个字符串参数,我希望它是一个具有所有必要值的 JSON

PUT /api/v1/carBrands/

Whereas in the second approach in the second scenario I have an endpoint per type of car brand, and each endpoint has a typed dto object representing all the values it needs.而在第二种情况的第二种方法中,我为每种类型的汽车品牌都有一个端点,每个端点都有一个类型化的 dto 对象,表示它需要的所有值。

PUT /api/v1/carBrand/1
PUT /api/v1/carBrand/2
.
.
.
PUT /api/v1/carBrand/n

The first approach seems to save a lot of repetitive code - afterall the only difference is the set of parameters.第一种方法似乎节省了很多重复的代码——毕竟唯一的区别是参数集。 However , since this accepts an arbitrary string, there is no way for the enduser to know what he should pass - he will need someone to tell it to him and/or read from documentation.但是,由于这接受任意字符串,最终用户无法知道他应该通过什么 - 他需要有人告诉他和/或从文档中阅读。

The second approach is a lot more readable, and any one can fill in the data, since they know what it is.第二种方法更具可读性,任何人都可以填写数据,因为他们知道它是什么。 But it involves mostly replicating the same code around 20 times.但它主要涉及复制大约 20 次相同的代码。

Its really hard for me to pick an option, since both approaches have their drawbacks.我真的很难选择一个选项,因为这两种方法都有其缺点。 How should I judge whats the better option我应该如何判断什么是更好的选择

I am wondering what is a better approach to the design for the endpoints of this API.我想知道设计此 API 端点的更好方法是什么。

Based on your examples, it looks as though you are asking about resource design, and in particular whether you should use one large resource, or a family of smaller ones.根据您的示例,您似乎在询问资源设计,尤其是您应该使用一种大型资源还是一系列较小的资源。

REST doesn't answer that question... not directly, anyway. REST 没有回答这个问题……无论如何也不是直接回答。 What REST does do is identify that caching granularity is at the resource level. REST 所做的是确定缓存粒度在资源级别。 If there are two pieces of information, and you want the invalidation of one to also invalidate the other, then those pieces of information should be part of the same resource, which is to say they should be accessed using the same URI.如果有两条信息,并且您希望一条信息的无效也使另一条无效,那么这些信息应该是同一资源的一部分,也就是说,应该使用相同的 URI 访问它们。

If that's not what you want, then you should probably be leaning toward using separated resources.如果这不是您想要的,那么您可能应该倾向于使用分离的资源。

I wouldn't necessarily expect that making edits to Ford should force the invalidation of my local copy of Ferrari , so that suggests that I may want to treat them as two different resources , rather than two sub-resources .我不一定期望对Ford进行编辑会迫使我的本地Ferrari副本失效,因此这表明我可能希望将它们视为两个不同的资源,而不是两个子资源

Compare相比

/api/v1/carBrands#Ford
/api/v1/carBrands#Ferrari

with

/api/v1/carBrands/Ford
/api/v1/carBrands/Ferrari

In the former case, I've got one resource in my cache (/api/v1/carBrands);在前一种情况下,我的缓存中有一个资源 (/api/v1/carBrands); any changes I make to it invalidate the entire resource.我对它所做的任何更改都会使整个资源无效。 In the latter case, I've got two resources cached;在后一种情况下,我缓存了两个资源; changing one ignores the other.改变一个忽略另一个。

It's not wrong to use one or the other;使用其中之一并没有 both are fine , and have plenty of history.两者都很好,并且有很多历史。 They make different trade offs, one or the other may be a better fit for the problem you are trying to solve today.他们进行了不同的权衡,其中一个可能更适合您今天要解决的问题。

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

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