架构师告诉你22条API设计的最佳实践
以下文章来源Java架构师技术,回复”Spring“获惊喜礼包
大家好,我是Java架构师
- 资源:资源是数据的一部分,例如:用户
- 集合:一组资源称为集合,例如:用户列表
- URL:标识资源或集合的位置,例如:/user
/systemOrders或/system_orders
/system-orders
/system-orders/{order_id}
/system-orders/{OrderId}
/system-orders/{orderId}
GET /user
GET /User
GET /users
GET /shops/:shopId/category/:categoryId/price
GET /shops/:shopId/或GET /category/:categoryId
POST /updateuser/{userId}
GET /getusers
PUT /user/{userId}
POST /alarm/245743/resend
{
user_name:
"Mohammad Faisal" user_id:
"1"}
{
userName:
"Mohammad Faisal" userId:
"1"}
product_order
product-orders
- API蓝图:https://apiblueprint.org/
- Swagger:https://swagger.io/
{
users: [
...
]
}
{
users: [
...
],
total: 34
}
GET /shops?offset=5&limit=5
GET /shops?fields=id,name,address,contact
GET /shops/123?token=some_kind_of_authenticaiton_token
Authorization: Bearer xxxxxx, Extra yyyyy
content-type: application/json
- GET /shops/2/products:从shop 2获取所有产品的列表。
- GET /shops/2/products/31:获取产品31的详细信息,产品31属于shop 2。
- DELETE /shops/2/products/31:应该删除产品31,它属于商店2。
- PUT /shops/2/products/31:应该更新产品31的信息,只在resource-URL上使用PUT,而不是集合。
- POST /shops:应该创建一个新的商店,并返回创建的新商店的详细信息。在集合url上使用POST。
- 当由于一个或多个服务错误而拒绝客户端请求时,一定要返回4xx HTTP错误代码。
- 考虑处理所有属性,然后在单个响应中返回多个验证问题。
- 扁平比嵌套好。
- 简单胜于复杂。
- 字符串比数字好。
- 一致性比定制更好。
译者:Mr.lzc,软件工程师、DevOpsDays、HDZ深圳核心组织者,目前供职于华为,从事云计算工作,专注于K8s、微服务领域。
最后,整理了300多套项目,赠送读者。扫码下方二维码,后台回复【赚钱】即可获取。
--END--
作者:刘志超来源:dockone.io/article/2434604原文:https://betterprogramming.pub/22-best-practices-to-take-your-api-design-skills-to-the-next-level-65569b200b9版权申明:内容来源网络,版权归原创者所有。除非无法确认,我们都会标明作者及出处,如有侵权烦请告知,我们会立即删除并表示歉意。谢谢!
往期惊喜:
扫码关注我们的Java架构师技术
带你全面深入Java
最新评论
推荐文章
作者最新文章
你可能感兴趣的文章
Copyright Disclaimer: The copyright of contents (including texts, images, videos and audios) posted above belong to the User who shared or the third-party website which the User shared from. If you found your copyright have been infringed, please send a DMCA takedown notice to [email protected]. For more detail of the source, please click on the button "Read Original Post" below. For other communications, please send to [email protected].
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。