Return a file stream from a REST controller in Spring Boot
Posted on Aug 14, 2015
•
Shaun Donnelly
•
•
If you’re using Spring Boot’s REST controllers for an application, your controller methods typically look like this:
Simple enough. Request comes in, JSON is returned on the response. But what if you want to return a file stream in your response?
It’s actually pretty easy, it’s just not very well documented. Just remove the produces block from your @RequestMapping annotation, and the @ResponseBody annotation from the method signature. Also, add a response as an argument to the method.
Finally, in the method copy an input stream to the response’s output stream: