Adding your own projection reference in spring data

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!

+6
source share
2 answers

I think the simplest and most correct thing is to use a child projection and parse your own client-side link.

0
source

This is a little old question, but today I ran into the same problem and found a solution:

All you have to do is @Projection annotation @Projection from the built-in projection (ProductProjection), which is used inside the top-level projection (PricedProductProjection).

Of course, in this case you cannot use this projection in GET requests, but I assume that you do not even need it. (It was created specifically for this)

0
source

All Articles