spring webflux返回application/stream+json

本文主要研究下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则调用端无法滚动得到结果,将一直阻塞等待数据流结束或超时。

输出

  • 后台输出
2018-02-08 21:36:49.701  INFO 1270 --- [ctor-http-nio-2] reactor.Flux.Map.1                       : onSubscribe(FluxMap.MapSubscriber)2018-02-08 21:36:49.702  INFO 1270 --- [ctor-http-nio-2] reactor.Flux.Map.1                       : request(1)2018-02-08 21:36:50.208  INFO 1270 --- [     parallel-2] reactor.Flux.Map.1                       : onNext(Price(timestamp=1518097010208, value=120.0))2018-02-08 21:36:50.229  INFO 1270 --- [ctor-http-nio-2] reactor.Flux.Map.1                       : request(31)2018-02-08 21:36:50.708  INFO 1270 --- [     parallel-2] reactor.Flux.Map.1                       : onNext(Price(timestamp=1518097010708, value=124.0))2018-02-08 21:36:51.208  INFO 1270 --- [     parallel-2] reactor.Flux.Map.1                       : onNext(Price(timestamp=1518097011208, value=119.0))2018-02-08 21:36:51.707  INFO 1270 --- [     parallel-2] reactor.Flux.Map.1                       : onNext(Price(timestamp=1518097011707, value=120.0))2018-02-08 21:36:52.207  INFO 1270 --- [     parallel-2] reactor.Flux.Map.1                       : onNext(Price(timestamp=1518097012207, value=109.0))2018-02-08 21:36:52.707  INFO 1270 --- [     parallel-2] reactor.Flux.Map.1                       : onNext(Price(timestamp=1518097012707, value=101.0))2018-02-08 21:36:53.208  INFO 1270 --- [     parallel-2] reactor.Flux.Map.1                       : onNext(Price(timestamp=1518097013208, value=114.0))2018-02-08 21:36:53.707  INFO 1270 --- [     parallel-2] reactor.Flux.Map.1                       : onNext(Price(timestamp=1518097013707, value=113.0))2018-02-08 21:36:54.206  INFO 1270 --- [     parallel-2] reactor.Flux.Map.1                       : onNext(Price(timestamp=1518097014206, value=105.0))2018-02-08 21:36:54.708  INFO 1270 --- [     parallel-2] reactor.Flux.Map.1                       : onNext(Price(timestamp=1518097014708, value=103.0))2018-02-08 21:36:55.208  INFO 1270 --- [     parallel-2] reactor.Flux.Map.1                       : onNext(Price(timestamp=1518097015207, value=123.0))2018-02-08 21:36:55.212  INFO 1270 --- [ctor-http-nio-2] reactor.Flux.Map.1                       : cancel()
  • 调用端输出
curl -i localhost:8080/stream
HTTP/1.1 200 OK
transfer-encoding: chunked
Content-Type: application/stream+json;charset=UTF-8{"timestamp":1518097010208,"value":120.0}
{"timestamp":1518097010708,"value":124.0}
{"timestamp":1518097011208,"value":119.0}
{"timestamp":1518097011707,"value":120.0}
{"timestamp":1518097012207,"value":109.0}
{"timestamp":1518097012707,"value":101.0}
{"timestamp":1518097013208,"value":114.0}
{"timestamp":1518097013707,"value":113.0}
{"timestamp":1518097014206,"value":105.0}
^C

可以看到由于使用了application/stream+json,返回的transfer-encoding是chunked,因此调用端可以做到滚动输出。

分页

使用了webflux之后,可能好奇之前的分页调用怎么办。reactive-streams是把数据当做数据流来用的,因此spring data reactive并不支持返回Page,但是调用参数可以传Pageable参数

public interface StocDao extends ReactiveCrudRepository<Stock, String> {

    Flux<Stock> findByName(String name,Pageable pageable);
}

注意这里返回Flux<Stock>,而不是Page<Stock> 也就是相当于丢失了total count

小结

对于webflux返回的Flux的流数据,需要配合返回MediaType.APPLICATION_STREAM_JSON_VALUE,同时调用端也需要能够支持这种mediaType(WebClient支持),这样才能启到reactive streams的效果。

doc

  • Serving large datasets with Spring WebFlux
  • mongo.reactive.repositories.usage
  • Is there any way to implement pagination in spring webflux and spring data reactive

原文发布于微信公众号 - 码匠的流水账(geek_luandun)

原文发表时间:2018-02-08

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏大大的微笑

java如何根据二进制流确定图片的类型

 为什么需要这样做? 因为仅仅通过后缀名我们并不能得知用户是否把图片的类型更改为其他类型. public enum ImageType { PNG('P','...

69660
来自专栏一“技”之长

iOS中使用NSAttributedString灵活创建标签

灵活使用NSAttributedString可以更轻松的创建出内容复杂的标签。需要注意一点:如果一个label设置了这个属性,那它其他的设置都将失效。

16410
来自专栏Android知识点总结

看得见的数据结构Android版之栈篇

进入和弹出动画为了好区分,用两个 ValueAnimator 控制,下面是成员变量

12520
来自专栏.Net移动开发

.Net语言 APP开发平台——Smobiler学习日志:如何仿微信朋友圈的消息样式?

8910
来自专栏Pythonista

Golang之json序列化(struct,int,map,slice)

11320
来自专栏Golang语言社区

Golang国际化(i18n)和本地化(l10n)指南

Go is a statically compiled language that gained a lot of popularity lately due ...

1.2K30
来自专栏菩提树下的杨过

Silverlight:Mouse Avoiding 躲避鼠标效果

昨晚在一国外博客上(从域名后缀pl上猜想应该是波兰)看到这种效果(Mouse Avoid 躲避鼠标),是基于Flash/AS3开发的,这个示例把弹性运动,摩擦力...

22570
来自专栏HansBug's Lab

2463: [中山市选2009]谁能赢呢?

2463: [中山市选2009]谁能赢呢? Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 830  Solv...

27740
来自专栏跟着阿笨一起玩NET

提取HTML的正文类

本文转载:http://blog.csdn.net/cjh200102/article/details/6824895

51410
来自专栏互联网开发者交流社区

FineUI控件集合

17270

扫码关注云+社区

领取腾讯云代金券