Return 方法 return 类型为 Mono 时的字符串值 <String>

Return a String value when method return type is Mono<String>

我的方法 return 是对控制器的 Mono<String> 响应。但是,我在尝试 return 一个 String 值时得到以下 error

Required Type: Mono<String>, 
Provided: Mono<Object>
no instance(s) of type variable(s) T exist so that Mono<T> conforms to String inference variable R has incompatible bounds: equality constraints

服务:

public Mono<String> moveToDB(String pid, String op, MoveRequest moveRequest) {
        return Mono.just(pid)
                .subscribeOn(Schedulers.boundedElastic())
                .map(id -> persistenceService.withTransaction((s, em) -> {
                     //some code goes here
                    if (condition)
                       return "data sync failed"; //ERROR
                    return "data synced successfully"; //ERROR
        }));
}

我是响应式编程的新手,我可能犯了一个愚蠢的错误,但我希望能得到一些帮助。谢谢!

我猜方法 withTransaction() 没有 return String 它应该的那样。您应该将 return 值映射到 String:

.map(id -> service.withTransaction((s, em) -> {
    //some code goes here
    if (true)
        return "data sync failed";
    return "data synced successfully";
}))
.map(e -> e.toString()); //or whatever