【问题标题】:Adding self link to projection in spring data rest在春季数据休息中添加自我链接到投影
【发布时间】:2016-09-01 19:23:27
【问题描述】:

我有一个包含 Products、Prices 和 PricedProducts 的简单应用程序。

当我请求 PricedProducts 列表时,我希望内联价格和产品。我用@RestResource(exported = false) 标记了我的 Price 存储库,所以对于这个存储库来说它工作正常。然而,产品需要是独立的实体(例如,我需要能够使用相同的产品构建多个 PricedProducts)。

我为 PricedProduct 创建了一个投影,将其添加为 excerptProjection,然后 GET 到 /priceProducts 返回:

{
  "_embedded": {
    "pricedProducts": [
      {
        "price": {
          "value": "100.50",
          "currency": "EUR"
        },
        "product": {
          "name": "Poatato",
          "description": null,
          "pictureUrl": null
        },
        "_links": {
          "self": {
            "href": "http://localhost:4200/api/v1.0/pricedProducts/1"
          },
          "pricedProduct": {
            "href": "http://localhost:4200/api/v1.0/pricedProducts/1{?projection}",
            "templated": true
          },
          "product": {
            "href": "http://localhost:4200/api/v1.0/pricedProducts/1/product"
          }
        }
      }
    ]
  },
  "_links": {
    "self": {
      "href": "http://localhost:4200/api/v1.0/pricedProducts"
    },
    "profile": {
      "href": "http://localhost:4200/api/v1.0/profile/pricedProducts"
    }
  }
}

这内联了我的产品,但它不提供自我链接。因此,在我的客户端应用程序中,例如,当有人编辑产品名称时,我不知道我必须更新哪个产品,除非我提出额外请求。

我接下来要做的是为 Product 创建一个投影,我在 PricedProduct 的投影中使用它。对 /priceProducts 的 GET 现在产生:

{
  "_embedded": {
    "pricedProducts": [
      {
        "price": {
          "value": "100.50",
          "currency": "EUR"
        },
        "product": {
          "pictureUrl": null,
          "description": null,
          "name": "Potato",
          "_links": {
            "self": {
              "href": "http://localhost:4200/api/v1.0/products/1{?projection}",
              "templated": true
            }
          }
        },
        "_links": {
          "self": {
            "href": "http://localhost:4200/api/v1.0/pricedProducts/1"
          },
          "pricedProduct": {
            "href": "http://localhost:4200/api/v1.0/pricedProducts/1{?projection}",
            "templated": true
          },
          "product": {
            "href": "http://localhost:4200/api/v1.0/pricedProducts/1/product"
          }
        }
      }
    ]
  },
  "_links": {
    "self": {
      "href": "http://localhost:4200/api/v1.0/pricedProducts"
    },
    "profile": {
      "href": "http://localhost:4200/api/v1.0/profile/pricedProducts"
    }
  }
}

现在我的产品有一个自我链接,但它指向它的投影 (http://localhost:4200/api/v1.0/products/1{?projection})。我想要的是:

{
  "_embedded": {
    "pricedProducts": [
      {
        "price": {
          "value": "100.50",
          "currency": "RON"
        },
        "product": {
          "pictureUrl": null,
          "description": null,
          "name": "Potato",
          "_links": {
            "self": {
              "href": "http://localhost:4200/api/v1.0/products/1
            }
          }
        },
        "_links": {
          "self": {
            "href": "http://localhost:4200/api/v1.0/pricedProducts/1"
          },
          "pricedProduct": {
            "href": "http://localhost:4200/api/v1.0/pricedProducts/1{?projection}",
            "templated": true
          },
          "product": {
            "href": "http://localhost:4200/api/v1.0/pricedProducts/1/product"
          }
        }
      }
    ]
  },
  "_links": {
    "self": {
      "href": "http://localhost:4200/api/v1.0/pricedProducts"
    },
    "profile": {
      "href": "http://localhost:4200/api/v1.0/profile/pricedProducts"
    }
  }
}

谢谢!

【问题讨论】:

  • 您找到解决方案了吗?

标签: spring spring-data-rest


【解决方案1】:

我认为最简单、更正确的做法是使用子投影并在客户端解析 self 链接。

【讨论】:

    【解决方案2】:

    这是一个有点老的问题,但我今天遇到了同样的问题,我找到了解决方案:

    您所要做的就是从顶层投影 (PricedProductProjection) 中使用的嵌入式投影 (ProductProjection) 中删除 @Projection 注释。

    当然,在这种情况下,您不能在 GET 查询中使用该投影,但我假设您甚至不需要它。 (它是专门为此目的而创建的)

    【讨论】:

      猜你喜欢
      • 2013-03-23
      • 2016-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-23
      • 1970-01-01
      • 2018-11-16
      相关资源
      最近更新 更多