在弹簧数据休息中添加自我链接到投影

Vla*_*riu 6 spring spring-data-rest

我有一个简单的应用程序,包含产品,价格和PricedProducts.

当我请求PricedProducts列表时,我想要内联价格和产品.我用@RestResource(exported = false)标记了我的价格库,所以对于这个它可以正常工作.但是,产品需要是自立实体(例如,我需要能够使用相同的产品构建多个PricedProducts).

我为PricedProduct创建了一个投影,将其添加为excerptProjection,并将一个GET添加到/ pricingProducts:

{
  "_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"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

这内联我的产品,但它不提供自我链接.因此,在我的客户端应用程序中,当有人编辑产品的名称时,例如,我不知道必须更新哪个产品,除非我做了额外的请求.

我接下来要做的是为Product创建一个投影,我在PricedProduct的投影中使用它.GET to/pricingProducts现在产生:

{
  "_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"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

现在我的产品有一个自我链接,但它指向它的预测(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"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

谢谢!

ape*_*lli 0

我认为最简单、更正确的做法是使用子投影并解析客户端的自链接。