I'm new to Spring WebFlux and I am trying to insert multiple rows to DB.
I'm creating a REST API for inserting data to DB.
I have a service for saving Flux object.
public Flux<FileUpload> create(FileUpload fileUpload) {
Flux<FileUpload> attachments = Flux.just(fileUpload)
.map(upload -> {
new FileUpload(upload);
this.fileUploadRepository.save(upload);
return upload;
});
return attachments;
}
And I have a RestController for Post request.
@PostMapping()
public Flux<FileUpload> saveFile(@RequestBody FileUpload fileUpload) {
return this.fileUploadService.create(fileUpload);
}
Then I am passing this Json via requestbody in Postman
[
{
"filename": "test.pdf",
"task_id": 2,
"e_tag": 1234567890
},
{
"filename": "test2.pdf",
"task_id": 2,
"e_tag": 0987654321
}
]
My question is how to iterate over flux then save the data to the database.
If you are using spring data, then the ReactiveCrudRepository
exposes a method which accepts a Flux
as an argument and saves all to the db.
<S extends T> Flux<S> saveAll(Publisher<S> entityStream);
Please let me know if you are not using spring data and I will update this answer.