简体   繁体   English

了解Spring Webflux /反应式端点

[英]Understanding Spring Webflux/Reactive endpoints

I'm trying to make sense of reactive endpoints in Spring, and this is confusing me. 我试图在Spring中理解反应式端点,这使我感到困惑。

// in a @RestController
@GetMapping("/someendpoint")
fun someEndpoint(): ResponseEntity<Mono<Map<String, Any>>> {
    return ResponseEntity(Mono.zip(
        Mono.fromRunnable<List<SomeItem>> { someApiCall() },
        Mono.fromRunnable<List<SomeOtherItem>> { someOtherApiCall() },
    ).map { tuple ->
        val someItems = tuple.t1
        val someOtherItems = tuple.t2
        mapOf(
            "someItems" to someItems,
            "someOtherItems" to someOtherItems
        )
    }, HttpStatus.OK)
}

I have an endpoint that returns a Mono of a Map, the idea being just to render some arbitrary JSON. 我有一个返回Map的Mono的终结点,这个想法只是呈现一些任意的JSON。

As part of the request, I do a Mono.zip(Mono.fromRunnable { ... }) where I call API's for data. 作为请求的一部分,我执行一个Mono.zip(Mono.fromRunnable {...}),在其中调用API的数据。

After the zip, I do a .map over the results and ultimately return the Map that I expect to see rendered. 压缩后,我在结果上创建一个.map,最终返回我希望看到的Map。

When I hit the endpoint, no JSON is rendered, but the runnables in the zip are called and the response time takes those API calls into account. 当我命中端点时,不会呈现JSON,但是会调用zip中的runnable,并且响应时间将这些API调用考虑在内。 The .map after the .zip is never called. .zip之后的.map永远不会被调用。 I'm probably just not understanding something simple here--like the .map is not subscribed to? 我可能只是在这里不理解一些简单的东西-就像.map没有订阅?

What is going on here and what is the usual way to handle a situation like this using Webflux/Reactor? 这是怎么回事,使用Webflux / Reactor处理这种情况的常用方法是什么?

正如Rene在评论中提到的那样,我的错误是使用Mono.fromRunnable {}而不是Mono.fromSupplier {}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM