Spring REST controller may send a serialized object, raw data or a file. Do you know how to define a file name in that third option? Check sixtieth question of full-stack dev quiz to verify if you know the answer.
There is a REST API controller created using Spring Framework. It returns ResponseEntity<Resource>
to return a file content. However, when entering the URL in a web browser, the content is saved as a file named file (because of the endpoint resource).
@RequestMapping("/file")
public ResponseEntity<Resource> getResource() {
...
return ResponseEntity.ok()
.body(resource);
}
Assume, that you are not ok with that. You want the file to be saved as dog.png. How can the controller push the desired file name to the client (browser)? Choose the best answer.
- a file name cannot be provided by the controller, because it is decided by the client (browser)
- change the method name from
getResource
togetDogPng
- set Content-Disposition header on ResponseEntity to attachment; filename=dog.png
- return
ResponseEntity<File>
instead ofResponseEntity<Resource>
- return
resource
not as a body, but as a file -ResponseEntity.ok().file(resource, "dog.png")
For the correct answer scroll down
.
.
.
.
.
.
The correct answer is: c. If you would like to read more, check Send file with REST API article.