问题
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:
- Create REST stream end-point.
- Link some service to that.
- This service will call third party API every 5 seconds using WebClient. And put data to my stream. (not sure about this step)
- 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