Spring 启动 webflux TEXT_EVENT_STREAM_VALUE 不工作
Spring boot webflux TEXT_EVENT_STREAM_VALUE is not working
我正在使用 spring-boot 依赖项 spring-boot-starter-webflux,
我想用浏览器每秒获取一个数据,
当我使用 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;
}));
}
}
我相信这会实现您的目标。
@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));
}
我正在使用 spring-boot 依赖项 spring-boot-starter-webflux, 我想用浏览器每秒获取一个数据, 当我使用 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;
}));
}
}
我相信这会实现您的目标。
@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));
}