猿问

如何修改 Mono 对象的属性而不在 Spring Boot 中阻塞它

我最近开始使用反应式并创建了一个使用反应式流的简单应用程序。


我有以下代码,我通过 empID 获得了一名员工。只有在showExtraDetailsboolean 设置为true. 如果它设置为 false 我必须在返回员工对象之前将额外的详细信息设置为 null。现在我在流上使用 aa 块来实现这一点。是否可以在没有阻塞的情况下执行此操作,以便我的方法可以返回 Mono。


以下是我完成的代码。


public Employee getEmployee(String empID, boolean showExtraDetails) {



    Query query = new Query();


    query.addCriteria(Criteria.where("empID").is(empID));



    Employee employee = reactiveMongoTemplate.findOne(query, Employee.class, COLLECTION_NAME).block();



    if (employee != null) {


        logger.info("employee {} found", empID);

    }



    if (employee != null && !showExtraDetails) {


        employee.getDetails().setExtraDetails(null);

    }


    return employee;


}  


jeck猫
浏览 224回答 1
1回答

侃侃无极

试试这个,应该像这样工作,假设reactiveMongoTemplate是你的 mongo 存储库return reactiveMongoTemplate.findById(empID).map(employee -> {            if (!showExtraDetails) {              employee.getDetails().setExtraDetails(null);            }            return employee;                        });
随时随地看视频慕课网APP

相关分类

Java
我要回答