如何转换单声道<响应态度< object>>单声道< vernestity< void>>

发布于 2025-02-04 08:59:55 字数 714 浏览 5 评论 0原文

嗨,我有一个api返回mono< wentermentity< void>,API将调用另一个返回mono< wentermentity< object> gt; gt; gt; 的API从其他API到mono< vernesponentity< void> 的结果?例如

public Mono<ResponseEntity<Void>> api() {
    return anotherApi()
        .map(response -> ResponseEntity.ok().build())
        .onErrorReturn(ResponseEntity.badRequest().build());
}

,如果我只有MAP,则可以使用以上有效,但是当我添加onerrorreturn时,它不投诉类型不匹配:无法从Mono&lt; wendersentity&lt; explionentity&lt; object; object&gt; &gt;对于单声道&lt; verneslentity&lt; void&gt;,为什么它成为mono&lt; wentermentity&lt; object&gt;&gt;&gt;

Hi I have an API returning Mono<ResponseEntity<Void>>, the API will call another API which returns Mono<ResponseEntity<Object>>, how do I convert the result from this other API to Mono<ResponseEntity<Void>>? e.g.

public Mono<ResponseEntity<Void>> api() {
    return anotherApi()
        .map(response -> ResponseEntity.ok().build())
        .onErrorReturn(ResponseEntity.badRequest().build());
}

The above works if I only have the map, but not when I add the onErrorReturn, it complains Type mismatch: cannot convert from Mono<ResponseEntity<Object>> to Mono<ResponseEntity<Void>>, why has it become Mono<ResponseEntity<Object>>?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

或十年 2025-02-11 08:59:55

代码是正确的,这只是无法正确推断键入的Java通用物。

您可以明确提供类型

public Mono<ResponseEntity<Void>> api() {
    return anotherApi()
        .<ResponseEntity<Void>>map(response -> ResponseEntity.ok().build())
        .onErrorReturn(ResponseEntity.badRequest().build());
}

,也可以移动onerrorreturn一个级别

public Mono<ResponseEntity<Void>> api() {
    return anotherApi()
        .onErrorReturn(ResponseEntity.badRequest().build())
        .map(response -> ResponseEntity.ok().build());
}

The code is correct and this is just Java generics that couldn't infer type correctly.

You could provide type explicitly

public Mono<ResponseEntity<Void>> api() {
    return anotherApi()
        .<ResponseEntity<Void>>map(response -> ResponseEntity.ok().build())
        .onErrorReturn(ResponseEntity.badRequest().build());
}

or move onErrorReturn one level up

public Mono<ResponseEntity<Void>> api() {
    return anotherApi()
        .onErrorReturn(ResponseEntity.badRequest().build())
        .map(response -> ResponseEntity.ok().build());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文