2016-05-06 3 views
5

私は、製品、価格、および価格製品を含む簡単なアプリケーションを持っています。スプリングデータの残りの部分に投影への自己リンクを追加する

私がPricedProductsのリストを要求するとき、私はPriceとProductをインライン化します。私は@RestResource(exported = false)で私の価格リポジトリをマークしました。このため、これは大丈夫です。しかし、製品は自立したエンティティである必要があります(たとえば、同じ製品を使用して複数のPricedProductを構築できる必要があります)。

私は、PricedProduct用の突起を作成しexcerptProjectionとして、それを追加、および/ pricedProductsリターンへのGET:

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

これは私の製品をインライン、しかし、それはそれのために自己のリンクを提供していません。だから私のクライアントアプリケーションでは、誰かが製品の名前を編集すると、たとえば、私は余分な要求をしない限り、私は更新が必要な製品がわからない。

私は、PricedProductの投影内で使用するProductの投影を作成しました。/pricedProductsへの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 {投影?})を指します。私が欲しいのです:

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

ありがとう!

+0

解決方法を見つけましたか? –

答えて

0

私は、最も簡単で、より正確なことは、子投影を使用し、クライアント側で自己リンクを解析することだと思います。

関連する問題