It turns out to be Spring Controller have default header for all the responses so that I have access to the response body but not the headers. To set the HttpHeaders, return a HttpEntity will work. Solution code below:
@RequestMapping(value = "/export", method = RequestMethod.GET, produces = "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
public HttpEntity<byte[]> export(@RequestParam(value = "domainName", required = true) String domainName,
@RequestParam(value = "projectName", required = true) String projectName,
@RequestParam(value = "testFolderId", required = true) int testFolderId) {
String exportedFileName = "exportedTC_" + domainName + "_" + projectName + "_"
+ Integer.toString(testFolderId) + ".docx";
SaveToZipFile saver = null;
ByteArrayOutputStream out = null;
HttpHeaders responseHeaders = null;
byte[] documentBody = null;
try {
extendExpiredALMSession();
saver = wordProcessor.ExportToWord(domainName, projectName, Integer.toString(testFolderId));
out = new ByteArrayOutputStream();
saver.save(out);
responseHeaders = new HttpHeaders();
responseHeaders.add("Content-Type",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document");
responseHeaders.add("Content-Disposition", "attachment; filename=\"" + exportedFileName + "\"");
documentBody = out.toByteArray();
} catch (RequestFailureException | RESTAPIException | InvalidDataException | UnLoginException
| UnAuthorizedOperationException | IOException | Docx4JException | URISyntaxException e) {
e.printStackTrace();
}
return new HttpEntity<byte[]>(documentBody, responseHeaders);
}
This works for me.