spring data rest - Can't POST a collection -
i have simple entity single collection mapped.
@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 application when include pagingandsortingrepository appointment. can phone call post command next payload.
{ "tracknumber" : "xyz123", "products": [ {"model" : "model", "description" : "name" }] }
when add together pagingandsortingrepository product , seek same post next 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 com.fasterxml.jackson.databind.jsonmappingexception: (was java.lang.nullpointerexception) (through reference chain: com.model.appointmentverification[\"products\"])" } payload both repositories returns this. desired format. link products should included { "tracknumber" : "xyz123", "_links" : { "self" : { "href" : "http://localhost:8080/consumerappointment/appointments/70" }, "products" : { "href" : "http://localhost:8080/consumerappointment/appointments/70/products" } }
with appointment repository next payload , can post list of products.
{ "tracknumber" : "xyz123", "products" : [ { "model" : "model", "description" : "name", } ], "_links" : { "self" : { "href" : "http://localhost:8080/consumerappointment/appointments/1" } } }
let's take step , create sure understand what's happening here: if repository detected, spring info rest exposes dedicated set of resources manage aggregates handled repository via http. thus, if have repositories multiple entities related each other, relationship represented link. why see products inlined appointmentrepository
in place , products
link in place 1 time create productrepository
.
if want expose both repositories resources, need hand uris of product
instances in payload post
create appointment
. means, instead of posting this:
{ "tracknumber" : "xyz123", "products": [ { "model" : "model", "description" : "name" } ] }
you'd create product
first:
post /products { "model" : "model", "description" : "name" } 201 created location: …/products/4711
and hand id of product appointment
payload:
{ "tracknumber" : "xyz123", "products": [ "…/products/4711" ]}
in case don't want of (no resources exposed product
in first place, utilize @repositoryrestresource(exported = false)
on personrepository
. still leave bean instance created repo, no resources exported , resource exposed appointment
s inlining related product
s.
spring-data-rest
No comments:
Post a Comment