简体   繁体   English

Spring 5 WebFlux Mono和Flux

[英]Spring 5 WebFlux Mono and Flux

In Spring 5 I just know Spring WebFlux Handler method handles the request and returns Mono or Flux as response. 在Spring 5中,我只知道Spring WebFlux Handler方法处理请求并返回MonoFlux作为响应。

@Component
public class HelloWorldHandler {
    public Mono<ServerResponse> helloWorld(ServerRequest request) {
        return ServerResponse.ok().contentType(MediaType.TEXT_PLAIN).body(BodyInserters.fromObject("Hello World"));
    }
}

But I have no idea what means Mono and Flux and how it works with the WebFlux Handler. 但我不知道MonoFlux是什么意思,以及它如何与WebFlux Handler协同工作。

Can any one simply explain 可以任何人简单解释一下

1.What means Mono and Flux . 1.什么意思是MonoFlux

2.How it works with the WebFlux Handler . 2.它如何与WebFlux处理程序一起使用

Thanks in advance. 提前致谢。

Webflux is all about reactive programming, which in summary means that business logic is only executed as soon as the data to process it is available (reactive). Webflux完全是关于反应式编程,总的来说,只有在处理数据时才能执行业务逻辑(反应性)。

This means you no longer can return simple POJO's, but you have to return something else, something that can provide the result when it's available. 这意味着您不再能够返回简单的POJO,但您必须返回其他内容,这些内容可以在可用时提供结果。 Within the reactive streams initiative, this is called a Publisher . 反应流计划中,这称为Publisher A Publisher has a subcribe() method that will allow the consumer to get the POJO when it's available. Publisher有一个subcribe()方法,允许消费者在POJO可用时获取它。

A Publisher (for example Publisher<Foo> ) can return zero or multiple, possibly infinite, results. Publisher (例如Publisher<Foo> )可以返回零或多个,可能是无限的结果。 To make it more clear how many results you can expect, Project Reactor (the reactive streams implementation of Pivotal) introduced two implementations of Publisher : 为了更清楚地说明您可以期待多少结果, Project Reactor (Pivotal的反应流实现)引入了两个Publisher实现:

  • A Mono , which will complete after emitting a single result. Mono ,在发出单个结果后将完成。
  • A Flux , which will emit zero or multiple, possibly infinite, results and then completes. 一个Flux ,它将发出零或多个,可能是无限的结果,然后完成。

So, basically you can see Mono<Foo> as the reactive counterpart of returning Foo and Flux<Foo> as the reactive counterpart of Collection<Foo> . 所以,基本上你可以看到Mono<Foo>作为返回FooFlux<Foo>的反应对应物作为Collection<Foo>的反应对应物。

For example: 例如:

Flux
    .just(1, 2, 3, 4)
    .map(nr -> nr * 2)
    .subscribe(System.out::println);

Even though the numbers are already available (you can see them), you should realize that since it's a Flux , they're emitted one by one. 即使数字已经可用(你可以看到它们),你应该意识到,因为它是一个Flux ,它们是逐个发射的。 In other cases, the numbers might come from an external API and in that case they won't be immediately available. 在其他情况下,这些数字可能来自外部API,在这种情况下,它们不会立即可用。 The next phase (the map operator), will multiply the number as soon as it retrieves one, this means that it also does this mapping one by one and then emit the new value. 下一个阶段( map运算符)将在检索到数字后立即将该数字相乘,这意味着它也会逐个执行此映射,然后发出新值。

Eventually, there's a subscriber (there should always be one, but it could be the Spring framework itself that's subscribing), and in this case it will print each value it obtains and print it to the console, also, one by one. 最终,有一个订阅者(应该总是有一个订阅者,但它可能是订阅的Spring框架本身),在这种情况下,它将打印它获得的每个值并将其打印到控制台,也是一个接一个。

You should also realize that there's no particular order when processing these items. 您还应该意识到处理这些项目时没有特定的顺序。 It could be that the first number has already been printed on the console, while the third item is hasn't been multiplied by two yet. 可能是第一个数字已经打印在控制台上,而第三个数字尚未乘以2。

So, in your case, you have a Mono<ServerResponse> , which means that as soon as the ServerResponse is available, the WebFlux framework can utilize it. 因此,在您的情况下,您有一个Mono<ServerResponse> ,这意味着只要ServerResponse可用,WebFlux框架就可以使用它。 Since there is only one ServerResponse expected, it's a Mono and not a Flux . 由于预期只有一个ServerResponse ,它是Mono而不是Flux

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

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