In my spring-boot application, I have a GET end-point. When we call this GET endpoint, the application is sending a GET request to another service with RestTemplate and returns the same response file as the response of the GET request. With the below code I'm able to receive the response file. But I need to set the same headers that I have received to RestTempate request. How to do that.
@GetMapping(value = URL_CONTENT_ID, produces = {MediaType.APPLICATION_OCTET_STREAM_VALUE, APPLICATION_ZIP_VALUE,
MediaType.TEXT_HTML_VALUE})
@ControllerLogging
public ResponseEntity<StreamingResponseBody> getContentFile(@PathVariable String contentId) {
StreamingResponseBody responseBody = outputStream -> {
getContentFile(outputStream, contentId);
outputStream.close();
};
return ResponseEntity.ok()
.body(responseBody);
}
public void getContentFile(OutputStream outputStream, String nodeId) {
RequestCallback requestCallBack = request -> {
HttpHeaders headers = new HttpHeaders();
authenticationHelper.apply(headers::set);
request.getHeaders().addAll(headers);
};
ResponseExtractor<OutputStream> responseExtractor = clientHttpResponse -> {
InputStream inputStream = clientHttpResponse.getBody();
StreamUtils.copy(inputStream, outputStream);
return null;
};
restTemplate.execute(dcmUrl + nodeId, HttpMethod.GET, requestCallBack, responseExtractor);
}
Please find the solution to this issue, the change I did was adding "HttpServletResponse" to the parameters of the controller method.