【问题标题】:Add custom default sub paths to endpoints in RAML将自定义默认子路径添加到 RAML 中的端点
【发布时间】:2015-10-14 20:37:08
【问题描述】:

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

/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

有没有更好的办法?

【问题讨论】:

    标签: rest raml


    【解决方案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 移动到一个独立的文件中。

    【讨论】:

    • 感谢您的回复。您的解决方案是有效的,并且在某种程度上更优雅,但它并不比我提出的更简单。我正试图专注于代码减少,因为我有很多资源与这些端点。
    猜你喜欢
    • 2019-06-30
    • 2019-07-18
    • 2015-09-26
    • 2019-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多