I would like to create/return an excel file from my RestEasy webservice but am having some trouble getting it to work. When i run the code below (pseudo code) I get the following error:
org.jboss.resteasy.core.NoMessageBodyWriterFoundFailure: Could not find MessageBodyWriter for response object of type: java.io.FileOutputStream of media type: application/vnd.ms-excel
Here is some code
@POST
@Path("/exportMyData")
@Produces("application/vnd.ms-excel")
public Response getMyData(@FormParam("id") String id) {
    HSSFWorkbook hwb = new HSSFWorkbook();
    ResponseBuilder response = null;
    try{
        List<Alert> alertList= service.getAlerts(id);
        HSSFSheet sheet =  hwb.createSheet("new sheet");
        HSSFRow rowhead=   sheet.createRow((short)0);
        rowhead.createCell((int) 0).setCellValue("ID");
        rowhead.createCell((int) 1).setCellValue("Name");
        rowhead.createCell((int) 2).setCellValue("Age");
        for(Alert alert : alertList){
            HSSFRow row=   sheet.createRow((short)1);
            row.createCell((int) 0).setCellValue(alert.getId());
            row.createCell((int) 1).setCellValue(alert.getName());
            row.createCell((int) 2).setCellValue(alert.getAge());
        }
        FileOutputStream fos = new FileOutputStream("mystream.xls");
        hwb.write(fos);
        response = Response.ok(fos);            
        response.header("Content-disposition","attachment; filename=export.xls");
    }catch(Exception e){
    }
    return response.build();
}
Any ideas? Thanks in advance /Eric
I've found that if you do the following, it'll work:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
hwb.write(baos);
response = Response.ok(baos.toByteArray());
response.header("Content-disposition", "attachment; filename=export.xls");
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With