春季启动webflux TEXT_EVENT_STREAM_VALUE不起作用

时间:2019-08-20 06:10:53

标签: spring-boot spring-webflux

我正在使用带有依赖项spring-boot-starter-webflux的spring-boot,     我想通过浏览器每秒获取一个数据,     当我使用spring-boot版本2.1.4时,代码正在工作,     但2.1.5或更高版本不起作用,     我将在10秒后获取所有数据,而不是每秒获取一个数据

I want to get the reason,or others i should  do 


I find spring-boot update the dependency of netty in 2.1.5,
so if i add the dependency in my pom.xml with 
<dependency>
    <groupId>io.projectreactor.netty</groupId>
    <artifactId>reactor-netty</artifactId>
    <version>0.8.8.RELEASE</version>
 </dependency>

it working


@RestController
@RequestMapping("/demo")
public class DemoController {
    // just get a string per second
    @GetMapping(value = "",produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String>  getMsg(){
       return Flux.fromStream(new Random().ints(10).mapToObj(intStream -> 
       {
           try {
               TimeUnit.SECONDS.sleep(1);
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
           return "this is data "+intStream;
       }));
    }
}

1 个答案:

答案 0 :(得分:0)

我相信这将实现您的目标。

@GetMapping(value = "",produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<String> getMsg(){
    return Flux.fromStream(new Random()
            .ints(10)
            .mapToObj(value -> "this is data " + value))
            .delayElements(Duration.ofSeconds(1));
}