简体   繁体   English

资源具有多个状态时如何设计REST API

[英]how to design REST API when resources have multiple states

I am going to design REST API for incident reporting in my web application. 我将设计用于我的Web应用程序中的事件报告的REST API。 An incident has three states. 事件具有三个状态。 ie investigation incident,resolved and postmortem. 即调查事件,解决和事后调查。 I use three database tables for each state.I want to design rest api for creating new incident to database. 我为每个状态使用三个数据库表。我想设计用于创建数据库新事件的rest api。 Here new incident means that incident in investigation state. 这里的新事件是指事件处于调查状态。

I have tried two approaches for designing rest api. 我尝试了两种设计Rest API的方法。 In first approach I consider three states as sub resources. 在第一种方法中,我将三种状态视为子资源。

POST - /incident/investigation
POST - /incident/resolved
POST - /incident/postmortem

In second approach, I think to pass incident state in http response of POST method. 在第二种方法中,我认为在POST方法的http响应中传递事件状态。

POST-/incident

What is the best way to design REST API? 设计REST API的最佳方法是什么?

A REST API should identify the resource, so you'd have something like /incident/123 to denote incident number 123. And /incident/123/status would be the current status of that particular incident. REST API应该标识资源,因此您将使用/incident/123符号来表示事件编号123。并且/incident/123/status将是该特定事件的当前状态。

As each incident is in exclusively one state, you don't have the "multiple states" problem. 由于每个事件仅处于一个状态,因此您不会遇到“多个状态”问题。

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

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