我最近开始使用反应式并创建了一个使用反应式流的简单应用程序。我有以下代码,我通过 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;}
1 回答
侃侃无极
TA贡献2051条经验 获得超10个赞
试试这个,应该像这样工作,假设reactiveMongoTemplate是你的 mongo 存储库
return reactiveMongoTemplate.findById(empID).map(employee -> {
if (!showExtraDetails) {
employee.getDetails().setExtraDetails(null);
}
return employee;
});
添加回答
举报
0/150
提交
取消