我想从我的 RestEasy 网络服务创建/返回一个 excel 文件,但在让它工作时遇到了一些麻烦。当我运行下面的代码(伪代码)时,出现以下错误:
org.jboss.resteasy.core.NoMessageBodyWriterFoundFailure:找不到 MessageBodyWriter 类型的响应对象:媒体类型的 java.io.FileOutputStream:application/vnd.ms-excel
这是一些代码
@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();
}
有任何想法吗?提前致谢/埃里克