如何在Spring Data Rest中添加POST / payments /:id / execute?

时间:2015-04-18 17:49:45

标签: spring-data-rest

简而言之,我想创建这些RESTful API端点

  1. POST /payments
  2. GET /payments/:id
  3. POST /payments/:id/execute =>向付款人收取费用将status更改为executed
  4. 1st2nd端点可以轻松实现,但如何实现3rd

1 个答案:

答案 0 :(得分:1)

如果您想使用Spring Data REST执行CRUD操作以外的操作,则需要Spring MVC使用define a custom controller。您的处理逻辑将进入控制器内部,您可以使用存储库自行处理任何资源保存。

更多Spring-data-rest-style(我还没有对此进行测试)的另一个想法是使用Spring Data REST events。这直接挂钩到系统中,可能允许您在不定义自己的控制器等的情况下完成额外的工作。

@RepositoryEventHandler 
public class TransactionEventHandler {

  @HandleBeforeSave
  public void handlePersonSave(Transaction incoming) {
    // call paypal, stripe, etc
    // flag incoming transaction as unsuccessful if the underlying transaction failed
  }
}