I have the below class that tries to return some data in the form of an excel spreadsheet. I'm getting the error
MessageBodyWriter not found for media type=application/octet-stream, type=class org.apache.poi.xssf.usermodel.XSSFWorkbook
I've also tried @Produces("application/vnd.ms-excel")
, but have gotten similar errors. Anyone have a suggestion as to how I can get this to return a spreadsheet? The last time I got an error message similar to this (complaining that a message body writer couldn't be found for arraylist) I just wrapped it in a generic entity. That trick didn't work this time.
@PermitAll
@Path("uploadWorkbook")
public class ExcelUploadResource {
@Context
ResourceContext resourceContext;
@Inject
JobService jobService;
@GET
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response list() {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("Job definitions");
int rowNum = 0;
for(Job job : jobService.list()){
Row row = sheet.createRow(rowNum++);
int cellNum = 0;
for(String field : job.toList()){
Cell cell = row.createCell(cellNum++);
cell.setCellValue(field);
}
}
GenericEntity<XSSFWorkbook> entity = new GenericEntity<XSSFWorkbook>(workbook) {};
ResponseBuilder response = Response.ok(entity);
response.header("Content-Disposition",
"attachment; filename=jobs.xls");
return response.build();
}
}