将 Flux<Document> 转换为 Flux<Object> 或 List<Object>

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

我有一个对象

class Employee {

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

还有一种方法 returns Flux ,Document 的类型是 org.bson.Document,示例

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

如何在 JAVA 中将 Flux 转换为 Flux 或 List

Flux API 是 Project Reactor library. If you wonder which operator fits your case, I would suggest you go through the Which operator do I need? 的一部分 官方参考指南部分。

在这种情况下,您需要以下 part:

I want to transform existing data:

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

因此,您需要 map 运算符将 Document 实例转换为 Employee

我用的就是这个。它工作正常。 studentService.getAll() returns Flux<Student> 使用地图我可以转换成 Flux<String>。您需要考虑@Ikatiforis 的回答。

在您的情况下,以下内容应该有效

documentFlux.map(d-> {
            Employee e = new Employee();
            //set values
            return e;
        });