Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SpringREST调用在浏览器中显示JSON——是否有一种方法允许用户下载该文件?_Java_Json_Spring_Rest - Fatal编程技术网

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”);
    返回计算机服务。检索所有计算机();
    }