Can't POST a collection

前端 未结 1 712
悲哀的现实
悲哀的现实 2021-01-05 15:20

I have a simple Entity with a single collection mapped.

@Entity
public class Appointment Identifiable   {  

    @Id
    @GeneratedValue(stra         


        
相关标签:
1条回答
  • 2021-01-05 15:35

    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.

    0 讨论(0)
提交回复
热议问题