I did not find a way to do this, the thread always closes. As a workaround, I created the following code:
public interface ResourceReader { void read(InputStream content); }
with the following implementation:
public class StreamResourceReader implements ResourceReader { private HttpServletResponse response; public StreamResourceReader(HttpServletResponse response) { this.response = response; } @Override public void read(InputStream content) { try { IOUtils.copy(content, response.getOutputStream()); } catch (IOException e) { throw new IllegalStateException(e); } } }
then in the controller:
@RequestMapping(value = "document/{objectId}") public void getDocumentContent(@PathVariable String objectId, HttpServletResponse response) { ResourceReader reader = new StreamResourceReader(response); service.readDocumentContent(objectId, reader); }
rest pattern call:
restTemplate.execute(uri, HttpMethod.GET, null, new StreamResponseExtractor(reader));
and line response extractor:
@Override public ResponseEntity extractData(ClientHttpResponse response) throws IOException { reader.read(response.getBody()); return null; }
and it works like a charm! :)
source share