简体   繁体   English

如何使用 REST API 和 HATEOAS 实现多步表单

[英]How to implement multi-step form with REST API and HATEOAS

I would like to create a REST API with using HATEOAS to create multi-step form.我想使用 HATEOAS 创建一个 REST API 来创建多步骤表单。 Each step will be a separate endpoint that offers the data to select, and in the last step, all the collected data will be stored in the database.每个步骤将是一个单独的端点,提供要选择的数据,在最后一步,所有收集的数据将存储在数据库中。 Is there any design pattern or best practice?是否有任何设计模式或最佳实践?

Thank you for answer.谢谢你的答案。

The idea that comes to my mind to solve this problem is to have different entities for each step of the compilation.我想到的解决这个问题的想法是为编译的每个步骤设置不同的实体。 That is, the starting point will have an action describing the first step (which could be an action or a link, with more or less descriptive list of fields to provide, according to the format you use), which will return a new entity with another action describing the second step, and so on until the final step is reached.也就是说,起点将有一个描述第一步的动作(可以是一个动作或一个链接,根据您使用的格式提供或多或少的描述性字段列表),它将返回一个新实体描述第二步的另一个动作,依此类推,直到到达最后一步。

Though, in order to respect REST/HATEOAS principles, I think this will force you to save entities on the server for each step (probably short-lived ones, that will be removed at the final step and "garbage-collected" by some cron job or the like to avoid partial compilation to clutter your storage), which may be quite clunky.不过,为了尊重 REST/HATEOAS 原则,我认为这将迫使您在服务器上为每个步骤保存实体(可能是短期实体,将在最后一步删除并由某些 cron 进行“垃圾收集”工作等,以避免部分编译使您的存储混乱),这可能非常笨重。 There could be some way to circumvent this, but I'm not sure how.可能有一些方法可以规避这一点,但我不确定如何。

Generally I would take this approach:通常我会采取这种方法:

  • Each step is its own form.每一步都是它自己的形式。
  • Each of those forms submit to a specific endpoint.这些表单中的每一个都提交到特定的端点。
  • The server stores the intermediate steps and returns the 'next step form'.服务器存储中间步骤并返回“下一步表单”。 You could store this in a storage system such as Redis.您可以将其存储在诸如 Redis 之类的存储系统中。
  • When the last form is submitted, take all the data from the intermediate steps and submit it.提交最后一个表单时,从中间步骤中获取所有数据并提交。

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

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