How to create multiple resources with a single query in Spring Data Rest?

What do I need to implement in order to create multiple records in my database with a single HTTP request?

I have a small web application using SpringDataRest and JPA / Hibernate in which I can create resources with such requests:

curl -XPUT -H"Content-Type: application/json; charset utf-8"\ -d'{"id":"1","type":"test"}'\ http://localhost:8080/test/items/1 

Instead, I would like to do something like:

 curl -XPUT -H"Content-Type: application/json; charset utf-8"\ -d'[{"id":"1","type":"test1"},{"id":"2","type":"test2"}]'\ http://localhost:8080/test/items/ 

The corresponding repository is as follows:

 @RestResource(path = "items", rel = "items") public interface ItemRepository extends PagingAndSortingRepository<Item, String> { } 

Used by Bean:

 @Entity @XmlRootElement(name = "page") @Table(name="page") public class Item { @Id @Column(name="id") private String id; @Column(name="type") private String type; public String getId() { return id; } public void setId(String id) { this.id = id; } /** * @return the type */ public String getType() { return type; } /** * @param type the type to set */ public void setType(String type) { this.type = type; } } 
0
java spring-data-rest hibernate jpa
source share
1 answer

You need a controller.

Essentially, you want to override the POST method on the resource to accept a list of elements instead of one.

You will need to get a link to your repository inside the controller and insert objects manually.

0
source share

All Articles