【发布时间】: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"
}
}
}
谢谢!
【问题讨论】:
-
您找到解决方案了吗?