1

I'm trying to send a POST request to save an entity in a Spring Data Rest repository.

The save is OK, but only for simple properties like Strings, etc...

But when I have to save a complex property (like a List<OtherEntity> otherEntities), the related object is not linked in the resource.

I'm trying to send a JSON in this format:

{
    "property": "value",
    "otherEntities" : "http://localhost:8080/myapp/api/otherEntities/1"
}

The object it's saved, but the link:

http://localhost:8080/myapp/api/objects/1/otherEntities

returns an empty array of "otherEntities" instead of the object reacheable at the url:

http://localhost:8080/myapp/api/otherEntities/1

Any idea it will be appreciated.

1 Answer 1

1

First of all, if you want to POST to a List property you have to surround it with []. So the JSON format would have to be:

{
    "property": "value",
    "otherEntities" : ["http://localhost:8080/myapp/api/otherEntities/1"]
}

Secondly, I suppose you are using @OneToMany or @ManyToMany relation. These annotations have a property "mappedBy" in one of the relation sides. Note that you can only save the relation from the side which not contains the property "mappedBy". If you save the relation from the other side, this would not be saved.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.