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?
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.
In your case following should work
documentFlux.map(d-> {
Employee e = new Employee();
//set values
return e;
});
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With