Java SpringREST调用在浏览器中显示JSON——是否有一种方法允许用户下载该文件?
我有一个基本的休息电话Java SpringREST调用在浏览器中显示JSON——是否有一种方法允许用户下载该文件?,java,json,spring,rest,Java,Json,Spring,Rest,我有一个基本的休息电话 /** * REST CALL * @return */ @RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) public List<Template> getTemplatesJson(){ logger.info("GET all computers- /computers/ -- content type = app
/**
* 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();
}
/**
*休息电话
*@返回
*/
@RequestMapping(method=RequestMethod.GET,products=MediaType.APPLICATION\u JSON\u VALUE)
公共列表getTemplatesJson(){
info(“获取所有计算机-/computers/--content type=application/json”);
返回计算机服务。检索所有计算机();
}
当我转到URL时,我会在浏览器中看到JSON。我们还希望我们的用户能够将JSON作为文件下载(稍后用于导入)
有没有一种简单的方法可以用这个端点实现这一点,或者我需要创建一个全新的端点 您可以使用
内容配置
标题向浏览器指示内容应作为附件下载,而不是显示
@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();
}
@RequestMapping(method=RequestMethod.GET,products=MediaType.APPLICATION\u JSON\u值)
公共列表getTemplatesJson(HttpServletResponse){
info(“获取所有计算机-/computers/--content type=application/json”);
setHeader(“内容处置”、“附件;文件名=templates.json”);
返回计算机服务。检索所有计算机();
}