How to pass binary data to response body in Spring WebFlux

I am doing a project using Spring WebFlux.

In the past, I used StreamingResponseBodyto stream responses back to the client, but I cannot find the equivalent in WebFlux.

Example:

import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;

@GetMapping("/video")
public StreamingResponseBody stream() {
    InputStream videoStream = ...
    StreamingResponseBody res = (os) -> { IOUtils.copy(videoStream, os); }
    return res;
}

Is there an equivalent StreamingResponseBodyfor WebFlux? or, should I import the traditional Spring MVC and mix them?

Change . While I solve this, referring to ServerHttpResponse(example below). But I'm still curious about the best solutions.

@GetMapping("/video")
fun stream2(response: ServerHttpResponse): Mono<Void> {
    val factory = response.bufferFactory()
    val publisher = videoStream
            .observeVideoParts()
            .map { factory.wrap(it.bytes) }
    return response.writeWith(publisher)
}
+4
source share

Source: https://habr.com/ru/post/1691277/


All Articles