How to post a list to Spring Data Rest?

Jim C picture Jim C · Jul 13, 2015 · Viewed 9.3k times · Source

I followed this example, which allows to post a unique Person object. I want a REST service where I can post a collection of Person at once, e.g. a list/any collection named Team with numerous Person objects in just one call.

I mean, my question is not exactly about the OneToMany relationship, where you send each person in a REST request. This topic is well answered.

I want to send a collection of Person objects taking advantage of @RepositoryRestResource or another feature from Spring Data Rest. Is this possible with Spring Data Rest or should I workaround by creating a controller, receive the list and parse the Team list to insert each Person?

I found this feature request, which seems to answer that nowadays Spring Rest Data is missing what I am looking for, but I am not sure.

In my business requirement, application A will post a list of orders to application B and I have to save it in database for future processing, so, after reading about Spring Data Rest and making some samples, I found its clean architecture amazing and very suitable for my requirement except for the fact that I didn't figure out how to post a list.

Answer

user1685095 picture user1685095 · Nov 26, 2015

Well, AFAIK you can't do that with spring data rest, just read the docs and you will see, that there is no mention about posting a list to collection resource.

The reason for this is unclear to me, but for one thing - the REST itself doesn't really specify how you should do batch operations. So it's unclear how one should approach that feature, like should you POST a list to collection resource? Or should you export resource like /someentity/batch that would be able to patch, remove and add entities in one batch? If you will add list how should you return ids? For single POST to collection spring-data-rest return id in Location header. For batch add this cannot be done.

That doesn't justify that spring-data-rest is missing batch operations. They should implement this IMHO, but at least it can help to understand why are they missing it maybe.

What I can say though is that you can always add your own Controller to the project that would handle /someentity/batch properly and you can even probably make a library out of that, so that you can use it in another projects. Or even fork spring-data-rest and add this feature. Although I tried to understand how it works and failed so far. But you probably know all that, right?

There is a feature request for this.