响应式 Spring 在 flatMap 后要求返回

huangapple go评论64阅读模式
英文:

Reactive Spring demanding return after flatMap

问题

我在使用IDE时遇到了一个错误提示缺少返回语句”,尽管我真的想在flatMap之后加一个then”。

repository.findById返回一个Flux<SomeObject>

setParent是一个bean更新操作

repository.save来自ReactiveCrudRepository<S extends T> Mono<S> save(S entity)

为什么要求我加上一个返回语句为什么我不能在这之后使用then()

```java
private Mono<Void> runThreeThings(Batch batch) {
  return repository.findById(batch.getId())
    .flatMap(doc -> {
      doc.setParent(SOME_PARENT_ID);
      return repository.save(doc);
    }) // 这是我遇到“缺少返回语句”错误的地方。为什么呢?

}
英文:

I'm getting a IDE error of "Missing return statement" although I really want to put a "then" after the flatMap.

The repository.findById return a Flux<SomeObject>.

The setParent is a bean update.

The repository.save is from the ReactiveCrudRepository: <S extends T> Mono<S> save(S entity)

Why am I being asked for a return? Why can't I use a then() after this?

private Mono&lt;Void&gt; runThreeThings(Batch batch) {
  return repository.findById(batch.getId())
    .flapMap(doc -&gt; {
      doc.setParent(SOME_PARENT_ID);
      repository.save(doc)
}) //  &lt;-- this is where I&#39;m getting a &quot;Missing return statement&quot; error. Why?

}

答案1

得分: 2

因为flatmap的参数是一个需要返回值的函数(该对象扩展自Mono),
在您的情况下,您需要返回一个Mono,解决方案如下。

private Mono<Void> runThreeThings(Batch batch) {
    return repository.findById(batch.getId())
            .flatMap(doc -> {
                doc.setParent(SOME_PARENT_ID);
                return repository.save(doc);
            });
}
英文:

Because flatmap has as parameter a function that need a return (object extends Mono)
in your case you need to return a mono and solution will be.

        private Mono&lt;Void&gt; runThreeThings(Batch batch) {
        return repository.findById(batch.getId())
                .flapMap(doc -&gt; {
                    doc.setParent(SOME_PARENT_ID);
                    return repository.save(doc);
                });

huangapple
  • 本文由 发表于 2020年10月8日 08:17:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/64254032.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定