当前位置:   article > 正文

springboot整合webflux,mono

springboot整合webflux,mono

1:pom.xml

 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-webflux</artifactId>
 </dependency>
 <dependency>
     <groupId>org.projectlombok</groupId>
     <artifactId>lombok</artifactId>
     <version>1.18.26</version>
 </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

2:controller控制类

package com.test.order.controller;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.awt.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;
import java.util.stream.Stream;

@RestController
@RequestMapping("/test")
@Slf4j
@CrossOrigin
public class TestController {


    @GetMapping("/one")
    public Mono<String> one() {
        log.info("mono1");
        Mono<String> hello = Mono.fromSupplier(() -> {
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return "one";
        });
        log.info("mono2");
        hello.subscribe(d -> System.out.println(d));
        return hello;
    }

    @SneakyThrows
    @GetMapping("/two")
    public String two() {
        System.out.println("******");
        TimeUnit.SECONDS.sleep(5);
        return "two";
    }


    @SneakyThrows
    @GetMapping("/three")
    public String three() {
        log.info("stringCompletableFuture1");
        CompletableFuture<String> stringCompletableFuture = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return "three";
        });
        log.info("stringCompletableFuture2");
        return "three";
    }

    @GetMapping(value = "/four",produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> four() {
        log.info("Flux1");
        Flux<String> flux = Flux.fromStream(

                IntStream.range(1, 5).mapToObj(d ->  {
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    return "flux data--" + d;
                })


        );
        log.info("Flux2");
        return flux;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86

3:主要返回的是event-source,返回的一直传送XMLHttpRequest.readyState的状态是3,发送完成才是4。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/538531
推荐阅读
相关标签
  

闽ICP备14008679号