将自定义默认子路径添加到RAML中的端点

时间:2015-07-24 08:45:22

标签: rest raml

在我的REST应用程序模型中,我的许多实体都包含一个“Auditable”接口。这迫使我将这些子路径添加到那些实体端点:

/myEntityResource:

  #... boring code ...

  /createdBy:
    get:
      responses:
        200:
          body:
            application/hal+json:
              example: !include samples/userAccount.json
  /lastModifiedBy:
    get:
      responses:
        200:
          body:
            application/hal+json:
              example: !include samples/userAccount.json

理想的解决方案应该是为端点添加“可审计”特征,但RAML定义特征仅适用于动词级别。

我的另一个理想选择应该是定义包含两个路径的资源类型“auditableItem”,但RAML定义不允许添加资源类型的路径。

目前我最好的方法是将两条路径添加到每个端点,并将主体包含在一个单独的文件中:

/createdBy: !include auditableBody.raml
/lastModifiedBy: !include auditableBody.raml

还有更好的方法吗?

1 个答案:

答案 0 :(得分:1)

简短的回答是"是"。

根据http://apiworkbench.com/docs/#creating-resource-type,您可以将其重构为:

#%RAML 1.0 
title: Auditable Example
version: 1

resourceTypes:
  Auditable-createdBy:
    get:
      responses:
        200:
          body:
            application/hal+json:
              # example: !include samples/userAccount.json
  Auditable-lastModifiedBy:
    get:
      responses:
        200:
          body:
            application/hal+json:
              # example: !include samples/userAccount.json

/myEntityResource:
  /createdBy: 
    type: Auditable-createdBy
  /lastModifiedBy: 
    type: Auditable-lastModifiedBy

当然,您可以将resourceTypes移动到一个独立的文件中。