1

I have a basic REST call

/**
 * REST CALL
 * @return
 */
@RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public List<Template> getTemplatesJson(){
    logger.info("GET all computers- /computers/ -- content type = application/json");
    return computerService.retrieveAllComputers();
}

When I go to the URL I see the JSON in the browser. We also would like our users to be able to download the JSON as a file (used for importing later on).

Is there an easy way I can do that with this endpoint or do I need to create a brand new endpoint?

Envin
  • 1,463
  • 8
  • 32
  • 69

1 Answers1

1

You can use the Content-Disposition header to indicate to the browser that the content should be downloaded as an attachment instead of being displayed.

  1. Stack Overflow on Content-Disposition
  2. Wikipedia section about Content-Disposition

Here's what your code might end up looking like:

@RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public List<Template> getTemplatesJson(HttpServletResponse response) {
    logger.info("GET all computers- /computers/ -- content type = application/json");
    response.setHeader("Content-Disposition", "attachment; filename=templates.json");
    return computerService.retrieveAllComputers();
}
Community
  • 1
  • 1
Erik Gillespie
  • 3,929
  • 2
  • 31
  • 48