Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Flux<Document> to Flux<Object> or List<Object>

I have an object of

class Employee {

    private String salary;
    private String empId;
    private String departmentId;
    private String status;
} 

and one method that returns Flux <Document>, Document is of type org.bson.Document, example

[
    {
        "empId": "B123",
        "salary": "1000",
        "departmentId": "winna",
        "status": "START"
    },
    {
        "empId": "A123",
        "salary": "2000",
        "departmentId": "dinna",
        "status": "COMPLETED"
    }
] 

How to convert Flux <Document> to Flux <Employee> or List <Employee> in JAVA?

like image 530
Shruti Gusain Avatar asked Sep 07 '25 02:09

Shruti Gusain


2 Answers

This one I am using. It is perfectly working fine. studentService.getAll() returns Flux<Student> using map I can convert into Flux<String>. You need to consider @Ikatiforis answer.

enter image description here

In your case following should work

documentFlux.map(d-> {
            Employee e = new Employee();
            //set values
            return e;
        });
like image 133
Prasath Avatar answered Sep 08 '25 15:09

Prasath


Flux API is part of the Project Reactor library. If you wonder which operator fits your case, I would suggest you go through the Which operator do I need? section of the official reference guide.

In this case, you need the following part:

I want to transform existing data:

on a 1-to-1 basis (eg. strings to their length): map (Flux|Mono)

So, you need the map operator to transform Document instance to Employee.

like image 25
lkatiforis Avatar answered Sep 08 '25 15:09

lkatiforis