Implementing Data Export
1. Implementing CSV Export
Example: Streaming CSV
@GetMapping(value = "/orders/export.csv", produces = "text/csv")
public ResponseEntity<StreamingResponseBody> export() {
StreamingResponseBody body = out -> {
try (var w = new OutputStreamWriter(out, UTF_8);
var p = new PrintWriter(w)) {
p.println("id,customer,total");
orderRepo.streamAll().forEach(o ->
p.printf("%s,%s,%d%n", o.id(), csv(o.customerName()), o.total().cents()));
}
};
return ResponseEntity.ok()
.header("Content-Disposition", "attachment; filename=orders.csv")
.body(body);
}
2. Implementing Excel Export
| Library | Detail |
| Apache POI (XSSF) | Full feature set; high memory |
| SXSSF | Streaming; large files |
| EasyExcel (Alibaba) | Lower memory than POI |
| Use cases | Formulas, multi-sheet, formatting |
3. Implementing PDF Export
| Library | Detail |
| OpenPDF | Fork of iText 4 (LGPL) |
| iText 7 | Powerful; AGPL/commercial |
| Apache PDFBox | Lower-level |
| Flying Saucer + Thymeleaf | HTML/CSS → PDF |
4. Implementing JSON Export
Example: Streaming JSON array
StreamingResponseBody body = out -> {
try (JsonGenerator g = mapper.createGenerator(out)) {
g.writeStartArray();
orderRepo.streamAll().forEach(o -> g.writeObject(toDto(o)));
g.writeEndArray();
}
};
5. Implementing Streaming Export for Large Datasets
| Pattern | Detail |
| Cursor / scroll | Avoid loading all into memory |
| JDBC fetchSize | Hint streaming reads (e.g., 1000) |
| Servlet streaming | StreamingResponseBody |
| Read-only TX | Lower DB overhead |
| Detach entities | Prevent JPA cache bloat |
6. Implementing Async Export Generation
| Step | Detail |
| Submit job | POST returns 202 + job ID |
| Worker | Generates file in background |
| Storage | Upload to S3 |
| Notify | Email or in-app on completion |
| Download | Pre-signed URL with expiry |
| Mechanism | Detail |
| Accept header | Content negotiation |
| Query param | ?format=csv |
| Path suffix | /export.csv |
| Allow-list | Reject unknown formats |
8. Implementing Export Compression
| Format | Detail |
| .gz | Single file |
| .zip | Multi-file bundle |
| .tar.gz | Unix-friendly multi-file |
| Streaming zip | ZipOutputStream |
| Approach | Detail |
| Multiple files | Split by row count |
| Date partitioning | One file per day/month |
| Manifest file | Lists all parts |
10. Handling Export Field Selection
| Mechanism | Detail |
| fields query | ?fields=id,total,status |
| Per-role projection | Hide sensitive columns |
| Templates | User-saved column sets |
11. Implementing Export Scheduling
| Feature | Detail |
| Cron schedule | Daily/weekly/monthly |
| Delivery | Email link, S3, SFTP |
| Filter set | Saved query |
| Failure alert | Notify owner on error |
12. Implementing Export Audit Trail
| Field | Detail |
| actor | Who exported |
| filters | What query |
| row count | How much data |
| file hash | Integrity |
| retention | Auto-delete after N days |
Warning: Bulk PII exports are a top breach vector — alert on anomalous export volumes.