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

时间:2016-05-06 16:21:38

标签: 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"
    }
  }
}

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

我接下来要做的是为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"
    }
  }
}

现在我的产品有一个自我链接,但它指向其投影(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"
    }
  }
}

谢谢!

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

这个问题有点老了,但是今天我遇到了同样的问题,并且找到了解决方案:

您要做的就是从嵌入的投影(ProductProjection)中删除std::array批注,该投影在顶级投影(PricedProductProjection)中使用。

当然,在这种情况下,您不能在GET查询中使用该投影,但是我认为您甚至不需要它。 (它是专门为此创建的)

相关问题