序
本文主要研究下spring webflux返回application/stream+json的实例
maven
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
controller
/**
* curl -i localhost:8080/stream
* @return
*/
@GetMapping(value = "/stream",produces = MediaType.APPLICATION_STREAM_JSON_VALUE)
public Flux<Price> priceStream(){
return Flux.interval(Duration.ofMillis(500))
.map(l -> new Price(System.currentTimeMillis(),ThreadLocalRandom.current().nextInt(100, 125)))
.log();
}
注意这里produces = MediaType.APPLICATION_STREAM_JSON_VALUE
如果不是application/stream+json则调用端无法滚动得到结果,将一直阻塞等待数据流结束或超时。