Unable to build

I have a simple Entity with a single collection display.

@Entity public class Appointment Identifiable<Integer> { @Id @GeneratedValue(strategy = GenerationType.AUTO) @JsonIgnore private Integer id; @Column(name="TRAK_NBR") private String trackNumber; @OneToMany(fetch =FetchType.EAGER, cascade= CascadeType.ALL) @JoinColumn(name="CNSM_APT_VER_WRK_I", nullable = false) private Set<Product> products = new HashSet<Product>(); } @Entity public class Product implements Identifiable<Integer> { @Id @Column(name = "CNSM_PRD_VER_WRK_I") @GeneratedValue(strategy = GenerationType.AUTO) @JsonIgnore private Integer id; @Column(name = "PRD_MDL_NBR") private String model; @Column(name = "PRD_SPEC_DSC") private String description; } 

In my application, when I only include PagingAndSortingRepository for the meeting. I can invoke the POST command with the following payload.

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

When I add a PagingAndSortingRepository for a product and try to execute the same POST, I get the following error message.

 { "cause" : { "cause" : { "cause" : null, "message" : null }, "message" : "(was java.lang.NullPointerException) (through reference chain: com..model.Appointment[\"products\"])" }, "message" : "Could not read JSON: (was java.lang.NullPointerException) (through reference chain: com.model.Appointment[\"products\"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: com.model.AppointmentVerification[\"products\"])" } My GET payload with both Repositories returns this. This is my desired format. The link to products should be included { "trackNumber" : "XYZ123", "_links" : { "self" : { "href" : "http://localhost:8080/consumerappointment/appointments/70" }, "products" : { "href" : "http://localhost:8080/consumerappointment/appointments/70/products" } } 

Only with the Appointment repository do I get the next payload and can post a list of products.

 { "trackNumber" : "XYZ123", "products" : [ { "model" : "MODEL", "description" : "NAME", } ], "_links" : { "self" : { "href" : "http://localhost:8080/consumerappointment/appointments/1" } } } 
+6
source share
1 answer

Let's take a step back and make sure that you understand what is happening here: if a repository is found, Spring Data REST provides a dedicated set of resources for managing aggregates processed by the repository via HTTP. Thus, if you have repositories for several objects related to each other, then the link appears as a link. That's why you see products nested only with the AppointmentRepository and the products link in place after creating the ProductRepository .

If you want to set both repositories as resources, you need to pass the URIs of the Product instances in the POST payload to create the Appointment . This means that instead of posting this message:

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

first create Product :

 POST /products { "model" : "MODEL", "description" : "NAME" } 201 Created Location: …/products/4711 

And then pass the product identifier to the Appointment payload:

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

In case you do not want this (without resources open to Product in the first place, use @RepositoryRestResource(exported = false) on PersonRepository ). This still leaves you with a bean created for the repo, but no resources are exported, and the resource open for Appointment is back to the inlining related Product s.

+4
source

Source: https://habr.com/ru/post/971191/


All Articles