quiz

Full-stack dev quiz question #60

Full-stack dev quiz question #60/span>quiz

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.

  1. a file name cannot be provided by the controller, because it is decided by the client (browser)
  2. change the method name from getResource to getDogPng
  3. set Content-Disposition header on ResponseEntity to attachment; filename=dog.png
  4. return ResponseEntity<File> instead of ResponseEntity<Resource>
  5. return resource not as a body, but as a file - ResponseEntity.ok().file(resource, "dog.png")

For the correct answer scroll down

.

.

 

 

 

 

 

 

 

 

 

 

Do not miss valuable content. You will receive a monthly summary email. You can unsubscribe anytime.

 

 

 

 

 

 

 

 

 

 

 

 

 

.

.

.

The correct answer is: c. If you would like to read more, check Send file with REST API article.

If you like what I do, consider buying me a coffee :)

Buy me a coffeeBuy me a coffee