Spring reactive streaming data from regular WebClient request

别说谁变了你拦得住时间么 提交于 2019-12-11 10:57:02

问题


Im learning new Spring WebFlux and reactive programming.

I would like to create reactive API that streams some data to Angular client. Part of this data I will take from another (third party) API that does not support streams.

So, as I understand I need to:

  1. Create REST stream end-point.
  2. Link some service to that.
  3. This service will call third party API every 5 seconds using WebClient. And put data to my stream. (not sure about this step)
  4. That stream is going to be returned by @RestController.

How can I implement this 4 steps?


回答1:


Assuming your remote service responds with a collection of POJOs that Jackson can deserialize as Something.class, you can do something like:

@GetMapping(path = "/streaming", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
@ResponseBody
public Flux<Something> streamSomething() {
  return WebClient.create()
    .get().uri("http://example.org/resource")
    .retrieve().bodyToFlux(Something.class)
    .delaySubscription(Duration.ofSeconds(5))
    .repeat();
}


来源:https://stackoverflow.com/questions/47063531/spring-reactive-streaming-data-from-regular-webclient-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!