Can't POST a collection

▼魔方 西西 提交于 2019-11-30 20:46:50

Let's take a step back and make sure you understand what's happening here: if a repository is detected, Spring Data REST exposes a dedicated set of resources for it to manage the aggregates handled by the repository via HTTP. Thus, if you have repositories for multiple entities related to each other, the relationship is represented as a link. This is why you see the products inlined with only the AppointmentRepository in place and the products link in place once you create a ProductRepository.

If you want to expose both repositories as resources, you need to hand the URIs of the Product instances in the payload for the POST to create an Appointment. That means, instead of posting this:

{ "trackNumber" : "XYZ123",
  "products": [
    { "model" : "MODEL",
      "description" : "NAME"
    }
  ]
}

you'd create a Product first:

POST /products
{ "model" : "MODEL",
  "description" : "NAME" }

201 Created
Location: …/products/4711

And then hand the ID of the product to the Appointment payload:

{ "trackNumber" : "XYZ123",
  "products": [ "…/products/4711" ]}

In case you don't want any of this (no resources exposed for Product in the first place, use @RepositoryRestResource(exported = false) on PersonRepository. That would still leave you with the bean instance created for the repo, but no resources exported and the resource exposed for Appointments back to inlining related Products.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!