0

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.

1 Answer 1

1

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.

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.