简体   繁体   English

为调用流程方法设计RESTful API

[英]Designing RESTful API for Invoking process methods

I would like to know how do design the RESTful web service for process methods. 我想知道如何为流程方法设计RESTful Web服务。 For example I want to make a REST Api for ProcessPayroll for given employee id. 例如,我想为给定的员工ID为ProcessPayroll创建一个REST Api。 Since ProcessPayroll is time consuming job, I don't need any response from the method call but just want to invoke the ProcessPayroll method asynchronously and return. 由于ProcessPayroll是一项耗时的工作,我不需要方法调用的任何响应,只是想异步调用ProcessPayroll方法并返回。 I can't use ProcessPayroll in the URL since it is not a resource and it is not a verb. 我不能在URL中使用ProcessPayroll,因为它不是资源而且不是动词。 So I thought that, I can go with the below approach 所以我想,我可以采用以下方法

Request 1 要求1

http://www.example.com/payroll/v1.0/payroll_processor POST http://www.example.com/payroll/v1.0/payroll_processor POST

body 身体

{ "employee" : "123" } {“employee”:“123”}

Request 2 要求2

http://www.example.com/payroll/v1.0/payroll_processor?employee=123 GET http://www.example.com/payroll/v1.0/payroll_processor?employee=123 GET

Which one of the above approach is correct one? 以上哪种方法是正确的? Is there any Restful API Design guidelines to make a Restful service for process methods and functions? 是否有任何Restful API设计指南可以为流程方法和函数提供Restful服务?

Which one of the above approach is correct one? 以上哪种方法是正确的?

Of the two, POST is closest. 在这两者中,POST最接近。

The problem with using GET /mumble is that the specification of the GET method restricts its use to operations that are "safe"; 使用GET / mumble的问题在于GET方法的规范将其用于限制为“安全”的操作; which is to say that they don't change the resource in any way. 也就是说他们不会以任何方式改变资源。 In other words, GET promises that a resource can be pre-fetched, just in case it is needed, by the user agent and the caches along the way. 换句话说,GET承诺,用户代理和沿途的缓存可以预先获取资源,以防万一需要。

Is there any Restful API Design guidelines to make a Restful service for process methods and functions? 是否有任何Restful API设计指南可以为流程方法和函数提供Restful服务?

Jim Webber has a bunch of articles and talks that discuss this sort of thing. 吉姆韦伯有一堆文章和谈话,讨论这种事情。 Start with How to GET a cup of coffee . 如何获得一杯咖啡开始

But the rough plot is that your REST api acts as an integration component between the process and the consumer. 但粗略的情节是,您的REST API充当流程和消费者之间的集成组件。 The protocol is implemented as the manipulation of one or more resources. 该协议被实现为对一个或多个资源的操纵。

So you have some known bookmark that tells you how to submit a payroll request (think web form), and when you submit that request (typically POST, sometimes PUT, details not immediately important) the resource that handles it as a side effect (1) starts an instance of ProcessPayroll from the data in your message, (2) maps that instance to a new resource in its namespace and (3) redirects you to the resource that tracks your payroll instance. 所以你有一些已知的书签告诉你如何提交工资单请求(想想网页表单),当你提交该请求时(通常是POST,有时是PUT,细节不是很重要),处理它作为副作用的资源(1) )从消息中的数据启动ProcessPayroll的实例,(2)将该实例映射到其命名空间中的新资源,以及(3)将您重定向到跟踪工资单实例的资源。

In a simple web api, you just keep refreshing your copy of this new resource to get updates. 在简单的Web API中,您只需不断刷新此新资源的副本即可获取更新。 In a REST api, that resource will be returning a hypermedia representation of the resource that describes what actions are available. 在REST API中,该资源将返回描述可用操作的资源的超媒体表示。

As Webber says, HTTP is a document transport application. 正如Webber所说,HTTP是一种文档传输应用程序。 Your web api handles document requests, and as a side effect of that handling interacts with your domain application protocol. 您的web api处理文档请求,并且该处理的副作用与您的域应用程序协议交互。 In other words, a lot of the resources are just messages.... 换句话说,很多资源只是消息....

We've come up with the similar solution in my project, so don't blame if my opinion is wrong - I just want to share our experience. 我们在我的项目中提出了类似的解决方案,所以如果我的意见不对,请不要责怪 - 我只想分享我们的经验。

What concerns the resource itself - I'd suggest something like 资源本身有什么问题 - 我建议像

http://www.example.com/payroll/v1.0/payrollRequest POST

As the job is supposed to be run at the background, the api call should return Accepted (202) http code. 由于作业应该在后台运行,api调用应该返回Accepted (202) http代码。 That tells the user that the operation will take a lot time. 这告诉用户操作将花费很多时间。 However you should return a payrollRequestId unique identifier ( Guid for example) to allow users to get the posted resource later on by calling: 但是,您应该返回一个payrollRequestId唯一标识符(例如Guid ),以允许用户稍后通过调用以获取发布的资源:

http://www.example.com/payroll/v1.0/payrollRequest/{payrollRequestId} GET

Hope this helps 希望这可以帮助

You decide the post and get on the basis of the API work- 您决定发布并获得API工作的基础 -

  1. If your Rest API create any new in row DB(means new resource in DB) , then you have to go for POST. 如果您的Rest API在行DB中创建任何新的(意味着DB中的新资源),那么您必须进行POST。 In your case if your payroll process method create any resource then you have to choose to POST 在您的情况下,如果您的工资核算流程方法创建任何资源,那么您必须选择POST

  2. If your Rest API do both, create and update the resources. 如果您的Rest API同时执行这两项操作,请创建和更新资源。 Means ,if your payroll method process the data and update it and create a new data , then go for PUT 意味着,如果您的工资单方法处理数据并更新数据并创建新数据,则转到PUT

  3. If your Rest API just read the data, go for GET. 如果您的Rest API只读取数据,请转到GET。 But as I think from your question your payroll method not send any data.So GET is not best for your case. 但是,正如我从您的问题中想出的那样,您的工资单方法不会发送任何数据。因此GET不适合您的情况。

As I think your payroll method is doing both thing. 我认为你的工资单方法正在做这两件事。

  1. Process the data , means updating the data and 处理数据,意味着更新数据和

  2. Create new Data , means creating the new row in DB 创建新数据意味着在DB中创建新行

NOTE - One more thing , the PUT is idempotent and POST is not.Follow the link PUT vs POST in REST 注意 - 还有一件事,PUT是幂等的,POST则不是。 在REST中关注PUT与POST的链接

So, you have to go for PUT method. 所以,你必须采用PUT方法。

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

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