@ResponseBody tells Spring MVC to write a handler method's return value straight into the HTTP response body instead of resolving it to a view name. For file downloads it matters because a byte[], Resource or InputStream-backed value is handled by an HttpMessageConverter and sent as raw bytes. It does not set the download headers, though. You still need Content-Type and Content-Disposition, which is why most production code returns ResponseEntity<Resource>.
The examples below target Spring Boot 3.x and 4.x on Java 21 or later, so they use the jakarta.* namespace and ContentDisposition from org.springframework.http.
In a plain @Controller, a returned String is treated as a view name. Adding @ResponseBody changes that: Spring picks a HttpMessageConverter that matches the return type and the requested media type, then writes the result to the response.
For files, three converters matter:
| Return type | Converter | Typical use |
|---|---|---|
byte[] | ByteArrayHttpMessageConverter | Small files already in memory |
Resource | ResourceHttpMessageConverter | Files on disk, classpath or in object storage |
StreamingResponseBody | Handled by Spring MVC's async support | Large files and generated content |
@RestController already includes @ResponseBody on every method, so you only add it explicitly in a @Controller class.
This version works, but the browser has no file name and may try to display the content inline.
The produces attribute sets the Content-Type. The whole file is read into the heap, so this is only suitable for small files.
ResponseEntity<Resource> gives you control over the status code and headers while keeping the body conversion that @ResponseBody provides. Use ContentDisposition to build a correct header, including a safely encoded file name.
What this does:
../../etc/passwd.application/octet-stream.Content-Disposition: attachment so browsers save the file instead of rendering it.Resource, so the file is streamed from disk rather than loaded fully into memory.Spring's ResourceHttpMessageConverter and the resource region support also handle Range requests for Resource bodies, which lets clients resume interrupted downloads.
When the content is generated on the fly, or you want to write to the output stream yourself, return StreamingResponseBody. Spring MVC runs it asynchronously so the request thread is not held during the transfer.
The response is written in chunks, so memory use stays flat no matter how large the export is.
byte[] for big files. The whole file sits on the heap for every concurrent request. Use Resource or StreamingResponseBody.Content-Disposition. Without attachment, browsers may open PDFs and images inline.ContentDisposition.builder handles this.@ResponseBody on a @RestController method. It is redundant, though harmless.No. Returning ResponseEntity<Resource> already implies body conversion. @ResponseBody is needed only when a @Controller method returns the file object directly. In a @RestController it is applied for you.
@ResponseBody only marks the return value as the body. ResponseEntity additionally lets you set the status code and headers such as Content-Disposition, Content-Length and caching headers, which downloads usually need.
Send Content-Disposition: attachment; filename="...". Build it with ContentDisposition.attachment().filename(name).build().
Return a Resource backed by a file or stream, or use StreamingResponseBody. Both write to the output stream in chunks.
@ResponseBody makes Spring write the return value of a handler as the response body, and message converters turn byte[] and Resource values into raw bytes. For real downloads, return ResponseEntity<Resource> with an explicit content type, length and Content-Disposition, validate the requested path, and switch to StreamingResponseBody when content is large or generated.