ResponseEntity< Mono>有什么区别?和Mono< ResponseEntity>作为休息控制器的返回类型 [英] what is the difference between ResponseEntity<Mono> and Mono<ResponseEntity> as a return type of a rest controller
问题描述
在Spring Webflux中,ResponseEntity< Mono>有什么区别?与Mono< ResponseEntity>作为休息控制器的返回类型?
In Spring Webflux, what's the difference between ResponseEntity<Mono> versus Mono<ResponseEntity> as a return type of a rest controller?
什么时候最合适?
在此问题之后,假设我需要返回一个列表,或者说Foo的几个元素,有很多返回Flux的示例.返回ResponseEntity< Flux>是否有意义还是Flux< ResponseEntity>?
Following up on this question, let's say I need to return a list, or let's say several elements of Foo, there are many examples of returning Flux. Would it make sense to return ResponseEntity<Flux> or Flux<ResponseEntity>?
当我寻找这个问题时,我在这里发现了同样的问题: https://github.com/spring-projects/spring-framework/issues/22614 ,但没有答案,我搜索了spring文档,但未找到任何信息.
When I'm looking for this question, I found the same question posted here: https://github.com/spring-projects/spring-framework/issues/22614, but no answer, I searched spring docs, but find no info.
感谢您的帮助.
推荐答案
以下是您可以使用 ResponseEntity
返回值进行的各种选择:
Here are the various options you can make with a ResponseEntity
return value:
-
ResponseEntity< Mono< T>>
或ResponseEntity< Flux< T>>
-这使响应状态和标头在提供主体时立即已知稍后异步进行.主体是Mono
还是Flux
取决于响应具有多少个值. -
Mono< ResponseEntity< T>>
-这在以后的一点上异步提供了全部三个-响应状态,标头和正文.IT允许响应状态和标头根据异步请求处理的结果而变化. -
Mono< ResponseEntity< Mono< T>>>或
Mono< ResponseEntity< Flux< T>>
也是可能的,但不太常见.它们首先异步地提供响应状态和标头,然后异步地提供响应主体,之后又异步地提供响应主体.
ResponseEntity<Mono<T>>
orResponseEntity<Flux<T>>
-- this makes the response status and headers known immediately while the body is provided asynchronously at a later point. Whether the body isMono
orFlux
depends on how many values the response has.Mono<ResponseEntity<T>>
-- this provides all three -- response status, headers, and body, asynchronously at a later point. IT allows the response status and headers to vary depending on the outcome of asynchronous request handling.Mono<ResponseEntity<Mono<T>>>
orMono<ResponseEntity<Flux<T>>>
are also possible but less common. They provide the response status and headers asynchronously first and then the response body, also asynchronously at a second point later.
这篇关于ResponseEntity< Mono>有什么区别?和Mono< ResponseEntity>作为休息控制器的返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!