I have a simple application containing products, prices and PricedProducts.
When I request a list of PricedProducts, I want the price and product to be embedded. I put my Price repository with @RestResource (exported = false), so it works fine for this. However, the products must be separate entities (I need to be able to create several PricedProducts using the same product, for example).
I created a projection for PricedProduct, added it as excerptProjection, and GET to / pricesProducts returns:
{ "_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" } } }
This applies to my product, however it does not provide an independent link for it. So, for example, in my client application, when someone edits a product name, I donβt know which product I should update, unless I make an additional request.
What I did next is to create a projection for the product that I use inside the projection for PricedProduct. Now GET to / pricesProducts gives:
{ "_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" } } }
Now my product has a link to itself, but it points to its projection ( http: // localhost: 4200 / api / v1.0 / products / 1 {? Projection}). I want to:
{ "_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" } } }
Thanks!