在Spring Data REST中向投影添加自身链接

6
我有一个简单的应用程序,包含产品、价格和定价产品。当我请求定价产品列表时,我希望价格和产品被内联。我用 @RestResource(exported = false) 标记了我的价格存储库,所以对于这个存储库来说它可以正常工作。然而,产品需要是独立的实体(例如,我需要能够使用相同的产品构建多个定价产品)。我为定价产品创建了一个投影,将其添加为摘要投影,并在 /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"
    }
  }
}

这会将我的产品内联,但它不为其提供自我链接。因此,在我的客户端应用程序中,当有人编辑产品的名称时,我不知道我必须更新哪个产品,除非我进行额外的请求。
接下来我做的是创建一个产品的投影,我在定价产品的投影中使用它。现在对/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{?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"
    }
  }
}

谢谢!


1
你找到解决方案了吗? - Charlie
2个回答

0

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

你需要做的就是从嵌入式投影(ProductProjection)中删除@Projection注释,该投影用于顶级投影(PricedProductProjection)中。

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


0

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接