6

I want to stream a file in a reactive way using spring webflux.
How my endpoint should look like more specific what is the type of the object ?

@GetMapping("/file")
Flux<???> file() {
    //Read file content into this ??? thing . 
}
shmuel buchnik
  • 133
  • 2
  • 9

1 Answers1

5

You can return a Resource instance like this:

@GetMapping("/file")
Mono<Resource> file() {
    //Create a ClassPathResource, for example
}

Note that this supports Byte Range HTTP requests automatically.

Brian Clozel
  • 56,583
  • 15
  • 167
  • 176
  • Would you mind commenting a little more on the automatic Byte Range request? If read the answer of bgraves in https://stackoverflow.com/questions/20634603/how-do-i-return-a-video-with-spring-mvc-so-that-it-can-be-navigated-using-the-ht what seems to state the opposite? – PeMa Jul 29 '18 at 18:51
  • 1
    bgraves mentioned https://jira.spring.io/browse/SPR-13834 which is now resolved. That question was about Spring MVC but the same applies now to Spring WebFlux (which came later) – Brian Clozel Jul 29 '18 at 18:57
  • Great, thank you a lot. That's good news. Now I just need to find out, how I make Videogular understand GridFsResource. Somehow this doesn't seem to work, while FileSystemResource worked. – PeMa Jul 29 '18 at 19:27
  • 1
    Does that resource implementation gives its content length information? If not, the resource range algorithm might opt out in that case. Try asking a new question for that showing a sample application. – Brian Clozel Jul 29 '18 at 19:32
  • Yes, it does have `contentLength()` method. However, I'll probably ask another question soon. Thanks for your hint anyway. – PeMa Jul 29 '18 at 19:34
  • Ok, works already. I missed `produces = "video/mp4"` option in the get mapping. Perfect. – PeMa Jul 29 '18 at 19:42