I have an AngularJS project that requests to a Spring Boot app.
Then the Spring Boot app will return data in bytes that can be later be downloaded as an Excel file like so:
Spring Boot controller app:
public ResponseEntity generateExcelReport(@RequestBody ArrayList<SearchCriteria> searchCriteriaList) {
Query dynamicQuery = new Query();
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.add("Content-Disposition", "attachment");
try {
List<Investment> resultList = investmentService.findByDynamicQuery(dynamicQuery);
Workbook workbook = myService.generateExcelReport(resultList);
ByteArrayOutputStream out = new ByteArrayOutputStream();
workbook.write(out);
byte[] bArray = out.toByteArray();
ByteArrayResource resource = new ByteArrayResource(bArray);
workbook.close();
out.close();
out.flush();
return ResponseEntity
.ok()
.headers(responseHeaders)
.contentLength(resource.getByteArray().length)
.contentType(MediaType.parseMediaType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"))
.body(resource);
} catch (IOException e) {
return ResponseEntity.badRequest().body(e);
}
}
But I want to improve this code by returning a ResponseEntity
with status 204 if the resultList
is empty like so:
Update :
public ResponseEntity generateExcelReport(@RequestBody ArrayList < SearchCriteria > searchCriteriaList) {
Query dynamicQuery = new Query();
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.add("Content-Disposition", "attachment");
try {
List < Investment > resultList = investmentService.findByDynamicQuery(dynamicQuery);
// If there are no results found, throw a 204 No Content back to front-end
// So that there is no Excel generation involved.
if (resultList.isEmpty()) {
return ResponseEntity.status(HttpStatus.NO_CONTENT).body("There are no results found.");
}
...
return ResponseEntity
.ok()
.headers(responseHeaders)
.contentLength(resource.getByteArray().length)
.contentType(MediaType.parseMediaType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"))
.body(resource);
} catch (IOException e) {
return ResponseEntity.badRequest().body(e);
}
}
This is the AngularJS Snippet for the front-end side
ReportService.generateExcelReport($scope.searchCriteriaList)
.then(function (responseObj) {
if (responseObj.status === 204) {
return SweetAlert.swal("No results", responseObj.data, "warning");
}
// responseObj.data = Excel bytes returned by Spring in this case
var blob = new Blob([responseObj.data], {
type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
});
FileSaver.saveAs(blob, "myreport.xlsx");
swal.close();
})
But my problem is, my $http.post request contains a responseType: "arraybuffer"
as part of the request.
Because of this, even if I return a String on the back-end side like this:
return ResponseEntity.status(HttpStatus.NO_CONTENT).body("There are no results found.");
I still get an arraybuffer in the response data. And I can't access the String.
$http.post request snippet:
generateExcelReport: function (searchCriteriaList) {
var requestConfig = {
responseType: "arraybuffer",
headers: {
"Content-Disposition": "attachment"
}
};
var url = "my-api/generate-paiwi-reports-excel"
return $http.post(url, searchCriteriaList, requestConfig);
}
I'm not sure if I'm getting into the right direction. Please help. =) Thank you very much in advance. =)