控制器端点命名约定

huangapple go评论85阅读模式
英文:

Controller Endpoint Naming Convention

问题

我有一个控制器端点,需要帮助确定命名规范。

基本上,该端点的作用是根据其 Id/Email(发送到请求的 URL 中)查找客户。因此,我需要有两个端点,一个用于 users/{id},另一个用于 users/{email}

问题是,上述方法行不通,因为路由完全相同。

我所做的是将 users/{email} 路由替换为:

users/get-by-email/{email},并将按 id 获取的路由保留为:users/{id}

谢谢

英文:

I have a controller endpoint and I need help with the naming convention.

Basically, the endpoint is supposed to find a customer by their Id/Email (which is sent in the url of the request). Thus I need to have two endpoints, one for users/{id} and one for users/{email}.

The issue is, the above wouldn't work as the routes are exactly the same.

What I have done is I have replaces the users/{email} route with:

users/get-by-email/{email} and kept the get by id route as: users/{id}

Thanks

答案1

得分: 2

似乎你正在创建一种用于与存储数据交互的 REST API。在 REST 中,你所处理的信息被称为 资源。你有 users 资源和 emails 资源。我的建议是你可以有一个 users/{id} 端点和一个 emails/{email} 端点,它们会返回所需的信息。此外,你还可以有一个 users/{id}/emails 端点,它会返回特定用户的电子邮件地址等信息。明智地命名端点的目的是为了达到清晰和自说明。

关于此的深入讨论请参见:Fielding,R. Representational State Transfer (REST)

英文:

It seems you are creating some kind of REST API for interacting with stored data. In REST, the information you are handling is called a resource. You have users resource and emails resource. My suggestion would be that you could have a users/{id} endpoint and emails/{email} endpoint, which would return the necessary information. Further, you could also have users/{id}/emails, which would return the email addresses of a specific user etc. The purpose of naming the endpoints wisely is to aim for clarity and self-explanation.

The most in-depth discussion: Fielding, R. Representational State Transfer (REST)

huangapple
  • 本文由 发表于 2020年7月24日 19:18:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63072498.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定