如何从 Spring Flux 平面图操作中 return 对象

How to return a object from Spring Flux flatmap operation

保存文件后,我正在寻找 return Mono.just(file.getAbsolutePath())。以下是我的代码:

 public Mono<String> save(Mono<FilePart> filePartMono) {
        Mono<String> monoString = filePartMono.flatMap(filePart -> {
            File file = new File(filePart.filename());
            if (file.exists()) {
                file.delete();
                LOG.info("existing file deleted: {}", file.getAbsolutePath());
            }
            Mono<Void> mono = filePart.transferTo(file);
            LOG.info("file saved: {}", file.getAbsolutePath());
            return Mono.just(file.getAbsolutePath());
        }).thenReturn("hello");
        return monoString;

现在我正在return说“你好”。有没有办法可以 return file.getAbsolutePath() 而不是我的 save() 方法中的字符串?

我觉得可以这样做:

public Mono<String> save(Mono<FilePart> filePartMono) {
    return filePartMono.flatMap(filePart -> {
        File file = new File(filePart.filename());
        if (file.exists()) {
            file.delete();
            log.info("existing file deleted: {}", file.getAbsolutePath());
        }
        return filePart.transferTo(file)
            .doOnNext(v -> {
                log.info("file saved: {}", file.getAbsolutePath());
            }).thenReturn(file.getAbsolutePath());
    });
}