2016-05-06 9 views
5

Ich habe eine einfache Anwendung, die Produkte, Preise und PricedProducts enthält.Hinzufügen von selbst Link zu Projektion im Frühjahr Daten Rest

Wenn ich die Liste der PricedProducts anfordere, möchte ich den Preis und das Produkt inline werden. Ich habe mein Preis-Repository mit @RestResource markiert (exported = false), also funktioniert es in Ordnung. Produkte müssen jedoch eigenständige Einheiten sein (ich muss in der Lage sein, mehrere PricedProducts mit demselben Produkt zu erstellen).

habe ich einen Vorsprung für PricedProduct, fügte sie als excerptProjection und ein GET/pricedProducts Rückgabe:

{ 
    "_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" 
    } 
    } 
} 

Das ist mein Produkt inlines, aber es nicht eine Selbst Link für sie liefern. Wenn ich beispielsweise in meiner Client-App den Namen des Produkts ändere, weiß ich nicht, welches Produkt ich aktualisieren muss, es sei denn, ich mache eine zusätzliche Anfrage.

Was ich als nächstes gemacht habe, war eine Projektion für das Produkt zu erstellen, die ich in der Projektion für PricedProduct verwende. Ein GET/pricedProducts jetzt ergibt:

{ 
    "_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" 
    } 
    } 
} 

Jetzt ist mein Produkt einen Selbst Link hat, aber es weist auf seinem Vorsprung (http://localhost:4200/api/v1.0/products/1 {Projektion?}). Was ich will ist:

{ 
    "_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" 
    } 
    } 
} 

Vielen Dank!

+0

Haben Sie eine Lösung dafür gefunden? –

Antwort

0

Ich denke, die einfachste und korrektere Sache ist, eine Kindprojektion zu verwenden und den Selbstlink auf der Klientenseite zu analysieren.

Verwandte Themen