我对 reactive programming 真的很陌生。我想弄清楚在这里做什么
我写了以下代码:
templateService.getTemplate(id)
.map(this::setTemplate)
.map(c -> convertDTOToBO(dto, c))
.flatMap(templateService::toPdf)
.map(content -> ResponseEntity.ok()
.header("Content-Disposition", "attachment; filename=\"test_" + NAMEPROPERTYHERE + ".pdf\"")
.body(content));
我需要在从 templateService.getTemplateById(id)
返回的对象上获取一个 value,需要将属性“名称”放在“文件名”上(我在这里写 NAMEPROPERTYHERE)。我只是不知道我该怎么做。
问题是当它到达最后一个 map 时,对象不再相同,我无法获得所需的 value。
我已经习惯了传统的编程方式,我只能想到将它传递给一个变量.. 最好的方法是什么?
回答1
您可以将逻辑“向下一级”以访问模板
templateService.getTemplate(id)
.map(template ->
setTemplate(template)
.map(c -> convertDTOToBO(dto, c))
.flatMap(templateService::toPdf)
.map(content -> ResponseEntity.ok()
.header("Content-Disposition", "attachment; filename=\"test_" + template.getName() + ".pdf\"")
.body(content))
);