本文介绍了RESTful API设计和CQRS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当时正在考虑如何使RESTFul API更具意图。我在各种博客上看到的一个常见问题是,传统的REST API会导致

I was thinking of how to make a RESTFul API more intention revealing. A common patter I see around various blogs on this is that conventional REST API results in

禁止播放器-> POST /播放器。

但是我要更改为更具意图的显示界面,我可以使用

But I were to change to a more intention revealing interface , I could use

Ban一个玩家-> POST / players / {playerid} / banPlayer

Ban a Player -> POST /players/{ playerid }/banPlayer

我觉得第二个是更多的意图

The second one I feel is more intention revealing.

我从团队中获得的共同异议是第二个不符合开始REST风格。

The common objection I get from the team is that the second one does not comply with start REST style.

目前,我还不能离开RESTful API。

Also currently I cannot move away from a RESTful API.

我想听听您对此的看法。

I would like to hear your thoughts on this.

推荐答案

通过Restful API设计,关于如何将 actions 应用于资源,有两种思路。

With Restful API design there are two schools of thought around how to apply actions to a resource.


  1. 您描述了对Uri中的资源要采取的措施:

  1. You describe the action to be taken against the resource in the Uri:

请求Uri:

POST / players / {id} / ba n

注意:只需使用 ban -我们已经知道资源了是玩家,位于基本Uri中。

Note: Just use ban - we already know the resource is a player, its in the base Uri.

您可以在请求正文中进行操作:

You can have the action in the body of the request:

请求Uri:

POST / players / {id}

请求正文:

{'action':'ban'}

您可以选择任一种方式-随便哪个,都有很多讨论,但最终都是正确的。

You can pick either way - whichever you prefer, there is lots of discussion on both but ultimately both are correct.

注意:

在这里我的假设是禁止玩家不只是更新其中的一部分,还涉及与玩家有关的系统动作(或状态转换)。否则,如果这仅仅是对播放器资源的更新,则应使用PATCH或PUT进行适当处理。

一些参考讨论:






  • http://restful-api-design.readthedocs.io/en/latest/methods.html
  • https://github.com/interagent/http-api-design/issues/58
  • https://nordicapis.com/designing-a-true-rest-state-machine/
  • https://softwareengineering.stackexchange.com/questions/141410/restful-state-changing-actions

如果您进行谷歌搜索还有更多... / em>

With plenty more if you do some Googling...

这篇关于RESTful API设计和CQRS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 10:20