Spring RestTemplate streaming response into another request

后端 未结 2 648
感情败类
感情败类 2021-02-04 13:47

I am trying to stream the result of a file download directly into another post using spring\'s RestTemplate

My current approach is the following:

         


        
2条回答
  •  眼角桃花
    2021-02-04 14:03

    If you want to forward the response directly without ever holding it in memory, you have to directly write to the response:

    @RequestMapping(value = "/yourEndPoint")
    public void processRequest(HttpServletResponse response) {
        RestTemplate restTemplate = new RestTemplate();
    
        response.setStatus(HttpStatus.OK.value());
    
        restTemplate.execute(
            fileToDownloadUri,
            HttpMethod.GET,
            (ClientHttpRequest requestCallback) -> {},
            responseExtractor -> {
                IOUtils.copy(responseExtractor.getBody(), response.getOutputStream());
                return null;
            });
    }
    

提交回复
热议问题